1.属性的直接赋值
1 //新用法:声明的同时可以初始化,并且允许只读属性初始化
2 class Student
3 {
4 public int StudentId { get; set; } = 1001;
5 public string Name { get; set; } = "张欣欣";
6 public int Age { get; set; } = 25;
7 public string Gender { get; } = "男";
8 }
2.字符串新用法:在字符串前面添加“$”前缀,(变量可以直接写到{}内,并且有很强的智能提示)
1 public void NewMethod()
2 {
3 Student objStudent = new NewDemo.Student();
4 string s1 = $"{objStudent.Name },{objStudent.Age }";
5 string s2 = $"姓名={objStudent.Name },年龄={objStudent.Age }";
6 Console.WriteLine($"{ s1} ,\r\n{ s2} ");
7
8 string s3 = $"{objStudent.Name,10},{objStudent.Age:d3}";
9 string s4 = $"{objStudent.Name,10},{objStudent.Age,10:d3}";
10 Console.WriteLine($"{ s3} ,\r\n{ s4} ");
11
12 string s5 = $"{DateTime.Now:yyyy-MM-dd}";
13 Console.WriteLine(s5);
14
15 //典型应用
16 string sql = $"select Name from Students where StudentId={objStudent.StudentId} and Age>{objStudent.Age }";
17 Console.WriteLine( sql);
18 }
3.lambda表达式在只读属性和表达式方法的新应用
1 /// <summary>
2 /// 表达式应用的新特性
3 /// </summary>
4 class ExpressionApp
5 {
6 //【1】表达式属性:只有一个get访问器的单行属性可以使用lambda语法编写
7 public DateTime Birthday { get; set; } = Convert.ToDateTime("1990-1-10");
8 //老表达式属性
9 public int Age0
10 {
11 get { return DateTime.Now.Year - Birthday.Year; }
12 }
13 //新表达式属性
14 public int Age1 => DateTime.Now.Year - Birthday.Year;
15
16 //【2】表达式方法:只有一条语句的方法可以使用lambda语法编写
17 //老表达式方法
18 public int Add0(int a, int b)
19 {
20 return a + b;
21 }
22 //新表达式方法
23 public int Add1(int a, int b) => a + b;
24 }
4.泛型集合的新初始化方法
1 /// <summary>
2 /// 泛型集合的新初始化方法
3 /// </summary>
4 class NewCollectionInit
5 {
6 public Dictionary<string, int> OldMethod()
7 {
8 Dictionary<string, int> student = new Dictionary<string, int>();
9 student.Add("张三", 25);
10 student.Add("李四", 34);
11 student.Add("王五", 26);
12 return student;
13 }
14 //新的初始化方法
15 public Dictionary<string, int> NewMethod()
16 {
17 Dictionary<string, int> student = new Dictionary<string, int>()
18 {
19 ["张三"] = 25,
20 ["李四"] = 34,
21 ["王五"] = 26
22 };
23 return student;
24 }
25 }
5.静态类直径using引入可直接写方法名,不需要写静态类.方法名
1 /// <summary>
2 /// static声明静态类的引用
3 /// </summary>
4 class StaticClassApp
5 {
6 //以前用法:两个数的绝对值相加
7 public static int OldMethod(int a, int b)
8 {
9 return Math.Abs(a) + Math.Abs(b);
10 }
11
12 //现在用法:使用using static System.Math;提前引入静态类,避免每次都调用Math类
13 public static int NewMethod1(int a, int b)
14 {
15 return Abs(a) + Abs(b);
16 }
17 public static int NewMethod2(int a, int b) => Abs(a) + Abs(b);
18 }
6.使用nameof
当参数变化时会在引用的地方同步变化,避免程序的硬编码
nameof里面可以是:类名、方法名、参数名、属性名
1 /// <summary>
2 /// nameof表达式
3 /// </summary>
4 class NameofExpressions
5 {
6
7 //以前用法:当参数名称变化的时候,被引用地方需要同步修改
8 public void OldMethod(int account)
9 {
10 if (account < 100)
11 {
12 throw new ArgumentException("参数account的值不能小于100!");
13 }
14 else
15 {
16 //其他操作...
17 }
18 }
19 //新用法:使用nameof,当参数变化时会在引用的地方同步变化,避免程序的硬编码
20 //nameof里面可以是:类名、方法名、参数名、属性名
21 public void NewMethod(int account)
22 {
23 if (account < 100)
24 {
25 throw new ArgumentException($"参数{nameof(account)}的值不能小于100!");
26 }
27 else
28 {
29 //其他操作...
30 }
31 }
32 }
7.null传递操作符:简化了空值的检查
以前三目操作运算符 (bool)?result1.result2 如果bool为真,返回result1,反之返回result2
现在 ?. 运算符 item?.Length 如果item为null,返回null,如果不为null,返回其长度
现在 ?. ?? 运算符 item?.Length ?? 0 如果item为null,返回0(??后面的值),如果不为null,返回其长度
1 /// <summary>
2 /// null操作符:null传递操作符简化了空值的检查
3 /// </summary>
4 class NullOperator
5 {
6 string[] sArray = new string[] { "bc", "cde", null, "efgg", null };
7
8 //以前用法
9 public void OldMethod()
10 {
11 foreach (string item in sArray)
12 {
13 var length = item == null ? 0 : item.Length;
14 Console.WriteLine(length);
15 }
16 Console.WriteLine("---");
17 }
18 //新方法:
19 public void NewMethod()
20 {
21 foreach (string item in sArray)
22 {
23 var length = item?.Length;//如果为null直接输出null
24 Console.WriteLine(length);
25 }
26 Console.WriteLine("---");
27 foreach (string item in sArray)
28 {
29 var length = item?.Length ?? 0;
30 Console.WriteLine(length);
31 }
32 }
33 }