1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| #pragma warning disable CS0219 //disable 禁用警告 Console.WriteLine("aaa");
Console.WriteLine("Hello, World!"); int x = 42; int y = 2; Console.WriteLine(int.MaxValue); long xx = -12345; Console.WriteLine(long.MaxValue); double d = 2.33; float z = 3.14F; decimal w = 19.99M; char letter = 'a'; string greeting = "111"; Console.WriteLine(greeting.Length); bool isActive = true; Console.WriteLine(isActive); Console.WriteLine("{0}", x); System.Console.WriteLine($"{x}"); string? c = Console.ReadLine(); Console.WriteLine(c); int? value = null; string? reference = null; Console.WriteLine(value); Console.WriteLine("{1} is not {0}", x, value);
x = int.Parse(greeting);
int Sum(int a, int b = 3) => a + b; Console.WriteLine($"Sum: {Sum(1)}"); void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } Console.WriteLine($"x={x},y={y}"); Swap(ref x, ref y);
Console.WriteLine($"x={x},y={y}");
static int Add(int a, int b, out int diff) { diff = a - b; return a + b; }
int sum = Add(5, 3, out int diff); Console.WriteLine($"和:{sum},差:{diff}");
bool result = int.TryParse("123", out int parsedValue); if (result) { Console.WriteLine($"Parsed Value: {parsedValue}"); } else { Console.WriteLine("Parsing failed."); }
int i = 0; while (i < 3) System.Console.WriteLine(i++);
int num = 1; switch (num) { case 1: Console.WriteLine(x); break; case 2: goto case 99; case 99: Console.WriteLine(233); break; }
object b = "abc"; b = 233; Console.WriteLine(b);
int[] numbers = [1, 2, 3, 4, 5]; var arr2 = new int[5]; Console.WriteLine($"Numbers: {string.Join(", ", numbers)}"); object[] mixedArray = [1, "two", 3.0, true]; int[] arr = [3, 1, 2]; Array.Sort(arr); Array.Reverse(arr); int index = Array.IndexOf(arr, 2); var sliced = arr[1..3]; int[] e = [1]; e = [2, 3]; Console.WriteLine(e.Length);
Dictionary<int, string> dictionay = new() { { 1, "one" }, { 2, "two" } }; Console.WriteLine(dictionay[1]);
var tuple1 = (10, "apple", 3.14); Console.WriteLine(tuple1.Item1); Console.WriteLine(tuple1.Item2);
var tuple2 = (Id: 100, Name: "Alice", Score: 95.5); Console.WriteLine(tuple2.Id); Console.WriteLine(tuple2.Name);
(string Name, int Age) GetPerson() { return ("Charlie", 30); } var (n, age) = GetPerson(); Console.WriteLine($"{n}, {age}");
var person = ("Alice", 30, "Female"); var (name, _, gender) = person; Console.WriteLine($"Name: {name}, Gender: {gender}");
Color favoriteColor = Color.Red & Color.Blue; var color = Color.Red | Color.Green; var allcolors = Color.Red | Color.Green | Color.Blue; Console.WriteLine(favoriteColor); Console.WriteLine(color); Console.WriteLine(allcolors); bool hasRed = color.HasFlag(Color.Red); var allinone = Color.All; if (allinone.HasFlag(Color.Green)) { Console.Write("Green"); } if (allinone.HasFlag(Color.Blue)) { Console.Write(" Blue"); } if (allinone.HasFlag(Color.Red)) { Console.Write(" Red"); }
[Flags] enum Color { white = 0, Red = 1 << 0, Green = 1 << 1, Blue = 1 << 2, All = ~0, }
#pragma warning restore CS0219 //restore 恢复 #pragma warning restore CS0162
|