且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.NetCore~C#6的一些新特性

更新时间:2022-08-14 20:30:38

在进行.netCore平台后,由于它的版本在.net4.6,C#6之后,所以它的语法也有一些新的特性,主要表现在以下几个方面

 只读属性初始化

static string Hello => @"Hello world , Lind!"; //static string Hello{get;}

属性初始化

 static DateTime AddTime { get; set; } = DateTime.Now;

字典初始化器

      static Dictionary<string, string> dictionary1 = new Dictionary<string, string>
        {
            ["name"] = "lind",
            ["age"] = "16"
        };

string.Format,后台引入了$,而且支持智能提示

 static string t2 = $"时间从{DateTime.Now}到{DateTime.Now.AddDays(1)}";

空对象判断

 static Test test = new Test();
 static string title = test?.Name;//if(test!=null) title=test.Name;

空集合判断

 static List<Test> testList = null;
 static Test defaultList = testList?[0];

方法-单行实现

 public void ConsolePrint(string msg) => Console.WriteLine(msg);

感谢各位的阅读与支持!

.NetCore,我们继续

本文转自博客园张占岭(仓储大叔)的博客,原文链接:.NetCore~C#6的一些新特性,如需转载请自行联系原博主。