加粗是个人认为对一般的应用程序比较有用的。
C#6.0
1.只读属性
属性使用lambda
旧: public string FullName { get() { return FirstName + “-” +GiveName;} }
public string FullName { get() => FirstName + “-” +GiveName; }
2.using static
旧 :MyStaticClass.StaticMethod();
新:命名空间中新增Using static MyStaticClass
在后续使用MyStaticClass的方法时可以直接写 MyStaticClass
3.null条件运算符号
旧:if( obj != null){ string name = obj.name;}
新:name = obj?.name; 如果obj为null 返回null给到name
name = obj?.name ?? “张三” 如果obj为null 返回"张三"给到name
4.字符串内插
旧:string sql = “select * from where id = '”+ id + " ’ ";
新:string sql = $"select * from where id = '{id} ’ ";
5.异常筛选器
try{}
Catch(Exception ex) when( ex.message.container(“Error0001”))
{
}
6.nameof表达式
String objstring = “EE”;
nameof(objstring )获取objstring 的类名 “String”
7.属性改变事件
典型的订阅模式
A 继承INotifyPropertyChanged
类中声明一个事件
public event PropertyChangedEventHandler PropertyChanged;
属性Set调用 PropertyChanged 事件
8.使用索引器关联初始化集合
Dictionary <Int,string> messages = new Dictionary <Int,string>
{
{1.“A”},
{2.“B”},
};
C#7.0
1.out变量 可以直接传参时申明,可以使用var
int.TryParse(“123”,out var resultint)
2.元组 //传输时使用方便
(int al,int beta) nameletters=(“a”,“b”);
nameletters.al=“aaa”;
public double X{get;set;}
public double Y{get;set;}
构造函数: Public Point(double x,double y) => (X,Y) = (x,y);
3.弃元
作为占位符
var (,A,B) = (“1”,“2”,“3”);
4.模式匹配
{
int input = 12 ;
int sum = 34;
//如果input是 int 则赋值给count
if(input is int count){sum += count}
}
在switch中
switch(i)
{
case 0: break;
case IEumerable childSequence:break;
//如果是 IEumerable 则把值放到 childSequence
case int n where n> 0 : break;
// 如果i是int 则放入n 如果n>0 执行break;
case null:break;
}
5.本地函数
public static string LocalFun(string param)
{
// 调用 LocalFun 会调用方法内的localf返回结果
return localf
string localf(string pa){ return pa; }
}
6.默认表达式
Fun<string ,bool> whereClause = default(Fun<string ,bool>);
Fun<string ,bool> whereClause =default;
int i = default; //直接获取默认值
string s = default;
7.命名参数
Method(param2:“B”,param1:“A”); //传参指明参数名字
8.访问修饰符
Public:公有的,是类型和类型成员的访问修饰符。对其访问没有限制。
Internal:内部的,是类型和类型成员的访问修饰符。同一个程序集中的所有类都可以访问
Private:私有的,是一个成员访问修饰符。只有在声明它们的类和结构中才可以访问。
Protected:受保护的,是一个成员访问修饰符。只能在它的类和它的派生类中访问。
protected internal:访问级别为 internal 或 protected。即,“同一个程序集中的所有类,以及所有程序集中的子类都可以访问。
Private Protected:访问限于包含的类和当前程序集中派生自包含的类。
10.throw表达式
可以放到表达式中,比如:
string arg = args.Length>=1? args[0] : throw new Exception(“Argument”);
11.通用的异步返回类型
ValueTask tasks = Func();
await foreach(var item in task )
{
}
12.数字文本语法改进
int i = 100_000; //可以用下划线间隔数字
C#8.0
1.ReadOnly成员
主要应用于结构体,不多写。
2.默认接口方法
现在可以将成员添加到接口,并为这些成员提供实现。
一头雾水,啥子作用,搞个和Abstract Class差不多的接口?
3.using 声明
旧:using (var file = new System.IO.StreamWriter(“WriteLines2.txt”))
{
…do something
}
新:using var file = new System.IO.StreamWriter(“WriteLines2.txt”);
4.异步流
它是用 async 修饰符声明的。
它将返回 IAsyncEnumerable。
该方法包含用于在异步流中返回连续元素的 yield return 语句。
5.异步可释放
语言支持实现 System.IAsyncDisposable 接口的异步可释放类型。
可使用 await using 语句来处理异步可释放对象。
6.Null合并复制
C# 8.0 引入了 null 合并赋值运算符 ??=。 仅当左操作数计算为 null 时,才能使用运算符 ??= 将其右操作数的值分配给左操作数。
C#9.0
1.记录
不可变的引用类型,Person实例化后不能修改。
public record Person
{
public string LastName { get; }
public string FirstName { get; }
public Person(string first, string last) => (FirstName, LastName) = (first, last);
}
2.仅限Init的资源库
3.顶级语句
旧:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Hello World!”);
}
}
}
新:
using System;
Console.WriteLine(“Hello World!”);