C# 7.0 新特征 Demo
发布时间:2020-12-16 01:24:59 所属栏目:百科 来源:网络整理
导读: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 pu
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 { 19 get => _age; 20 ///C#语句随意使用throw语句 21 set => _age = value <= 0 || value >= 130 ? throw new ArgumentException("参数不合法") : value; 22 } 23 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 { 39 40 } 41 42 43 //局部函数 44 public static void Outer() 45 { 46 WriteLine("局部方法:"); 47 Inner(5); 48 Inner(10); 49 Inner(12); 50 51 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 WriteLine("输入一个数字:"); 81 var input = ReadLine(); 82 if (int.TryParse(input,out var result)) 83 { 84 WriteLine("您输入的数字是:{0}",result); 85 } 86 else 87 { 88 WriteLine("无法解析输入..."); 89 } 90 } 91 92 public static void TestTuple() 93 { 94 WriteLine("Tuple::"); 95 96 ///默认 97 var tuple = (12,23); 98 WriteLine($"{tuple.Item1},{tuple.Item2}"); 99 100 ///左边=>解构 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 WriteLine($"int类型:{i},且数字大于等于10"); 146 break; 147 case float f: 148 WriteLine($"浮点类型:{f}"); 149 break; 150 case string str when int.TryParse(str,out int result): 151 WriteLine($"字符串类型转为Int类型:{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 WriteLine($"int类型:{i},且数字小于10"); 163 } 164 else if (item is int i2) 165 { 166 WriteLine($"int类型:{i2},且数字大于等于10"); 167 } 168 else if (item is float f) 169 { 170 WriteLine($"浮点类型:{f}"); 171 } 172 else if (item is string str && int.TryParse(str,out int result)) 173 { 174 WriteLine($"字符串类型转为Int类型:{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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在C#Windows窗体中向TableLayoutPanel添加一行
- 自定义控件的自定义的属性attrs.xml下的declare-styleable中
- VB.Net等同于C#’dynamic’with Option Strict On
- ruby-on-rails – 解析电子邮件附件w回形针 – 可能没有临时
- ruby-on-rails – 为什么rails在创建后会重定向到show动作?
- XML声明标记是否区分大小写?
- ios – Xcode 6 GM iPhone 6模拟器 – 不正确大小的故事板出
- Vue三种常用传值示例(父传子、子传父、非父子)
- 正则表达式元字符
- Cocos2d-x_CCControlSlider(滑动条类)介绍