c#6.0、7.0新增语法

c#6.0

一、自动属性初始化,属性可以自动赋初始值,例如:

public string Name { get; set; } = "summit";
public int Age { get; set; } = 22;
public DateTime BirthDay { get; set; } = DateTime.Now.AddYears(-20);
public IList<int> AgeList {get;set;} = new List<int> { 10, 20, 30, 40, 50 };


二、导入静态类
using static System.Math;//静态导入static类
导入静态类以后可以省略类名调用内部成员


三、字符串格式化,可以直接使用$进行字符串的嵌入了,例如:
2 Console.WriteLine($"年龄:{this.Age} 生日:{this.BirthDay.ToString("yyyy-MM-dd")}");
4 Console.WriteLine($"{(this.Age <= 22 ? "小鲜肉" : "老鲜肉")}");

四、空值运算符,如果是null值也可以调用方法了,这时程序什么都不会输出,例如:
1 int? iValue = 10;
2 Console.WriteLine(iValue?.ToString());//不需要判断是否为空
3 string name = null;
4 Console.WriteLine(name?.ToString()); // 程序不会报错,也不会输出任何值
5 Console.WriteLine("空值运算符");
结果:
这样就不需要像以前一样先判断一下变量是不是null值了。


五、对象初始化器
在前面一篇文章中讲过一次C#语法糖,在那里讲过对象初始化器和集合初始化器,以前初始化字典集合要像下面的方式:
1 IDictionary<int, string> dictOld = new Dictionary<int, string>()
2 {
3 { 1,"first"},
4 { 2,"second"}
5 };

c#6.0新增
1 IDictionary<int, string> dictNew = new Dictionary<int, string>()
2 {
3 [4] = "first",
4 [5] = "second"
5 };

六、异常过滤器,可以设置在满足某种条件的情况下,才能进入异常捕获,例如:
1 int exceptionValue = 10;
2 try
3 {
4 Int32.Parse("s");
5 }
6 catch (Exception e) when (exceptionValue > 1)//满足条件才进入catch
7 {
8 Console.WriteLine("catch");
9 }

七、nameof表达式
我们以前使用过typeof,typeof()可以获取类型,nameof表达式和typeof一样的,例如:
Console.WriteLine(nameof(peopleTest)); //peopleTest
Console.WriteLine(nameof(people.Name));//Name

八、在属性/方法里面使用Lambda表达式
public string NameFormat => “zhangsan”;//只读属性
public void Print() => Console.WriteLine(Name);

c#7.0

一、out输出参数
在以前使用out输出参数的时候,必须先定义变量,然后才能使用, 在C#7.0中,可以不用先定义,就能够直接使用了:
this.DoNoting(out int x, out int y);
this.DoNoting(out var l, out var m);


二、模式
public void PrintStars(object o)
{
if (o is null) return; // 常量模式 "null"
if (!(o is int i)) return; // 类型模式 定义了一个变量 "int i" i的值就是o的值 相当于is类型判断
Console.WriteLine($"i={i}");
}

除了可以像上面那样使用外,还可以使用下面的方式:
1 private void Switch(string text)
2 {
3 int k = 100;
4 switch (text)
5 {
6 case "Tom" when k > 10: // text="Tom"且k<10才会输出Tom
7 Console.WriteLine("Tom");
8 break;
9 case "Joe" when text.Length < 10: //text="Joe"且text的长度<10才会输出Joe
10 Console.WriteLine("Joe");
11 break;
12 case string s when s.Length > 7://模式 定义变量s,s就是text的值
13 Console.WriteLine(s);
14 break;
15 default:
16 Console.WriteLine("default");
17 break;
18 case null:
19 Console.WriteLine("null");
20 break;
21 }
22 }

三、元组
先来看下面的两个方法:
1 /// <summary>
2 /// 使用默认参数名称
3 /// </summary>
4 /// <param name="id"></param>
5 /// <returns></returns>
6 private (string, string, string) LookupName(long id) // tuple return type
7 {
8 return ("first", "middle", "last");
9 }
10
11 /// <summary>
12 /// 不使用默认参数名称
13 /// </summary>
14 /// <param name="id"></param>
15 /// <returns></returns>
16 private (string first, string middle, string last) LookupNameByName(long id) // tuple return type
17 {
18 return ("first", "middle", "last");
19 //return (first: "first", middle: "middle", last: "last");
20 }
调用:
1 // 使用默认参数名称:Item1、Item2、Item3
2 Console.WriteLine($"使用默认参数名称");
3 var result = this.LookupName(1);
4 Console.WriteLine(result.Item1+result.Item2+result.Item3);

7 // 不使用默认参数名称
8 Console.WriteLine($"不使用默认参数名称");
9 var result2 = this.LookupNameByName(1);
10 Console.WriteLine(result2.first+result2.middle+result2.last);

13 // 也可以使用默认参数名称
14 Console.WriteLine($"也可以使用默认参数名称");
15 Console.WriteLine(result2.Item1+result2.Item2+result2.Item3);
16 Console.WriteLine();
17 Console.WriteLine();


四、数字分割
如果数字太长,可以按照一定的位数用“_”进行分割,例如:
1 long big = 100_000_0000;
2 Console.WriteLine($"big={big}");

原文地址:https://www.cnblogs.com/fanfan-90/p/12141855.html