1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using static System.Console; 5 6 namespace ConsoleApp1 7 { 8 public class CSharp7 9 { 10 public static int b = 0b0001; 11 public static int b2 = 0b0001_00_00; 12 public static long salary = 1000_000_000; 13 public static decimal PI = . 3 .141_592_653_589m; 14 15 16 Private int _age; . 17 public int Age 18 is { . 19 GET => _age; 20 is /// C # statement disposable throw statement 21 is SET => = _age value <= 0 || value> = 130. ? the throw new new ArgumentException The ( " invalid parameters " ): value; 22 is } 23 is 24 public String Name { get; set; } = "jefft"; 25 26 /// <summary> 27 /// 解构方法 28 /// </summary> 29 /// <param name="name"></param> 30 /// <param name="age"></param> 31 public void Deconstruct(out string name, out int age) 32 { 33 name = Name; 34 age = Age; 35 } 36 37 [ public CSharp7 () 38 is { 39 40 } 41 is 42 is 43 is // local function 44 is public static void Outer () 45 { 46 is the WriteLine ( " local approach: " ); 47 Inner ( . 5 ); 48 Inner ( 10 ); 49 Inner ( 12 is ); 50 51 is void Inner ( int len =10) 52 { 53 for(var i = 0; i< len; i++) 54 { 55 Write($" {i},"); 56 } 57 58 WriteLine(); 59 } 60 } 61 62 ~CSharp7() => Console.WriteLine("Finalized!"); 63 64 65 public static void Instance() 66 { 67 var item = new CSharp7(); 68 item.Age = 30; 69 var (name, age) = item; 70 WriteLine($"解构结果:name:{name},age: {age}"); 71 } 72 73 public static void TestNumberForamt() 74 { 75 WriteLine($"pi={pi}, salary={salary}, b={b}, b2={b2}"); 76 } 77 78 public static void TestOutTypeVar () 79 { 80 the WriteLine ( " Enter a number: " ); 81 var INPUT = the ReadLine (); 82 IF ( int .TryParse (INPUT, OUT var Result)) 83 { 84 the WriteLine ( " Your digital input is: {0} " , Result); 85 } 86 the else 87 { 88 the WriteLine (" Unable to parse input ... " ); 89 } 90 } 91 is 92 public static void TestTuple () 93 { 94 the WriteLine ( " Tuple :: " ); 95 96 /// Default 97 var tuple = ( 12 is , 23 is ); 98 the WriteLine ($ " {} tuple.Item1, tuple.Item2 {} " ); 99 100 /// left => deconstruction 101 var (One, TWO) = (11, 22); 102 WriteLine($"{one},{two}"); 103 104 ///右边 105 var tuple2= (one: 1, two: 2); 106 WriteLine($"{tuple2.one},{tuple2.two}"); 107 } 108 109 public static void TestLoaclRef() 110 { 111 int[,] arr = { { 11, 15 }, { 22, 25 } }; 112 113 ref var num = ref GetLocalRef(arr, c => c % 2 == 0); 114 Console.WriteLine($"原数值{arr[1, 0]}"); 115 num = 600; 116 Console.WriteLine($"新数值{arr[1, 0]}"); 117 118 } 119 120 static ref int GetLocalRef(int[,] arr, Func<int, bool> func) 121 { 122 for (int i = 0; i < arr.GetLength(0); i++) 123 { 124 for (int j = 0; j < arr.GetLength(1); j++) 125 { 126 if (func(arr[i, j])) 127 { 128 return ref arr[i, j]; 129 } 130 } 131 } 132 133 throw new InvalidOperationException("Not found"); 134 } 135 136 137 public static void TestCaseTypeVar(object item) 138 { 139 switch(item) 140 { 141 case int i when i< 10: 142 WriteLine($"int类型:{i},且数字小于10"); 143 break; 144 case int i: 145 the WriteLine ($ " int type: {i}, and is a number greater than equal to 10 " ); 146 BREAK ; 147 Case a float F: 148 the WriteLine ($ " floating-point type: {F} " ); 149 BREAK ; 150 Case String STR When int .TryParse (STR, OUT int Result): 151 the WriteLine ($ " character string type int Switch type: Result {} " ); 152 BREAK ; 153 default : 154 BREAK ; 155 } 156 } 157 158 public static void TestIsTypeVar ( Object Item) 159 { 160. IF (Item IS int I && I < 10 ) 161 { 162 the WriteLine ($ " int type: {i}, and the number is smaller than 10 " ) ; 163 } 164 is the else IF (Item IS int I2) 165 { 166 The WriteLine ($ " int type: {i2}, and is a number greater than equal to 10 " ); 167 is } 168 the else IF (Item IS a float F) 169 { 170. the WriteLine ($ " floating-point type: {F} " ); 171 is } 172 the else IF (Item IS string STR && int .TryParse (STR, OUT int Result)) 173 { 174 the WriteLine ($ " character string type int Switch type: {result}"); 175 } 176 } 177 } 178 }
1 static void Csharp7Test() 2 { 3 CSharp7.TestIsTypeVar("77"); 4 CSharp7.TestCaseTypeVar("88"); 5 6 CSharp7.TestNumberForamt(); 7 CSharp7.Outer(); 8 CSharp7.Instance(); 9 CSharp7.TestTuple(); 10 CSharp7.TestLoaclRef(); 11 CSharp7.TestOutTypeVar(); 12 Console.ReadKey(); 13 }