1 using System;
2 using static System.Math;//using static,仅仅引入类中的静态方法
3 namespace _6._0Syntax
4 {
5 class Program
6 {
7 delegate bool Predicate(string str);
8 static void Main(string[] args)
9 {
10 /*nameof
11 string a = nameof(Program.Main);
12 */
13
14 /*String interpolation
15 var s = string.Format("{0} is so beauty{{~~~}}", "You");
16 Person p = new Person { Name = "senki", Age = 12 };
17 var s = $"{p.Name,20} is {p.Age:D3} year{{s}} old";
18 */
19
20 /*NULL操作
21 string str = null;
22 int? length = str?.Length;
23 char? c = str?[0];
24 int l = str?.Length ?? 0;
25 List<Person> list = null;
26 int? b= list?[0]?.Name?.Length;
27 Predicate predicate = null;
28 if (predicate?.Invoke(str) ?? false)
29 {
30 }
31 */
32
33 /*初始化时设定索引
34 var numbers = new Dictionary<int, string> {
35 [7]="liu",
36 [9]="zhan",
37 [15]="qi"
38 };*/
39
40 /* 异常过滤器
41 try
42 {
43 throw new CustomException();
44 }
45 catch (Exception e) when (IsCanCatch(e))
46 {
47 }
48 */
49 Max(1, 2);//通过using static,直接使用静态方法
50 Console.WriteLine(1);
51 Console.ReadLine();
52 }
53
54 //表达式函数
55 static void Print(string str) => Console.WriteLine($"{str}");
56
57 class Person
58 {
59 public string Name { get; set; } = "senki";//自动属性初始值设定
60 public bool Sex { get; } = true;
61 public int Age { get; set; }
62 public string LastName => Name;//只读属性
63 public char this[int i]=>Name?[i]??'A';//索引
64
65
66 }
67 class CustomException : Exception
68 {
69 public override string Message
70 {
71 get
72 {
73 return "测试异常";
74 }
75 }
76 }
77 private static bool IsCanCatch(Exception e)
78 {
79 if (e is CustomException)
80 return true;
81 return false;
82 }
83
84 struct School
85 {
86 public string Name { get; set; }
87 public int Age { get; }
88 public School(string name, int age)
89 {
90 Name = name;
91 Age = age;
92 }
93 //public School():this("",120)
94 //{ }
95
96 }
97
98
99 }
100 }