c# 6.0 学习笔记
2016-07-02 01:45
417 查看
refer :
http://www.cnblogs.com/yinrq/p/5600530.html http://www.cnblogs.com/wolf-sun/p/5168217.html http://www.cnblogs.com/wolf-sun/p/5172914.html
使用 vs2015 .net 4.6.1
1. nameof
好处 : 类似 enum 的作用, 当要批量替换属性名字时, 有智能提示, 这样就不怕漏换了.
2.null 处理
是 null 的话后面就不会继续跑, 会直接返回 null 值,
如果是 int 也会变成 nullable 哦,看上面的例子.
3. using static
好处 : 不需要写 ClassName
4. string interpolation
好处 : 早就好这样了, string.Format 和 "+ +" 一样乱丫, 不过还是 js 的 `` 更好
5. lambda in class method and properties
好处 : 好看一些吧
6. default value in class properties
好处 : 美
7. 字典
好处 : 比较贴近正常的赋值 data2["ket1"] = "value";
http://www.cnblogs.com/yinrq/p/5600530.html http://www.cnblogs.com/wolf-sun/p/5168217.html http://www.cnblogs.com/wolf-sun/p/5172914.html
使用 vs2015 .net 4.6.1
1. nameof
public class Demo { public string name { get; set; } } string value = nameof(Demo.name); //"name"
好处 : 类似 enum 的作用, 当要批量替换属性名字时, 有智能提示, 这样就不怕漏换了.
2.null 处理
public class Demo { public string name { get; set; } public int age { get; set; } public void doSomeThing() { } } Demo demo = null; //before if (demo != null) demo.doSomeThing(); //after demo?.doSomeThing(); //before string valuex = (demo != null) ? demo.name : "defaultValue"; //after string valuey = demo?.name ?? "defaultValue"; // for int int? age = demo?.age; int age = (demo?age).GetValueOrDefault();
是 null 的话后面就不会继续跑, 会直接返回 null 值,
如果是 int 也会变成 nullable 哦,看上面的例子.
3. using static
namespace Project { public class Demo { public static void method() { } } } using static Project.Demo; //before Demo.method(); //after method();
好处 : 不需要写 ClassName
4. string interpolation
string value1 = "kelly"; string value2 = "penny"; //before string value4 = string.Format("i love {0}, i have {1}", value1, value2); //after string value3 = $"i love {value1}, i hate {value2}"; //i love kelly, i hate penny
好处 : 早就好这样了, string.Format 和 "+ +" 一样乱丫, 不过还是 js 的 `` 更好
5. lambda in class method and properties
public class Demo { //before public string getString1(string value) { return $"i love {value}"; } //after public string getString2(string value) => $"i love {value}"; //before public string fullname1 { get { return getString1("kelly"); } } //after public string fullname => getString1("kelly"); //this feature only support get }
好处 : 好看一些吧
6. default value in class properties
//before public class Demo1 { public Demo1() { this.name = "value"; } public string name { get; set; } } //after public class Demo2 { public string name { get; set; } = "value"; }
好处 : 美
7. 字典
//before Dictionary<string, string> data1 = new Dictionary<string, string> { { "key1" , "value" }, { "key2" , "value" } }; //after Dictionary<string, string> data2 = new Dictionary<string, string> { ["key1"] = "value", ["key2"] = "value", };
好处 : 比较贴近正常的赋值 data2["ket1"] = "value";
相关文章推荐
- Lync(Skype)接口开发实录
- C# WinForm生成Code128条形码
- C#设计模式(一)订阅-发布模式
- C# 图片缩略
- juggle添加c#版本
- 弥补学生时代的遗憾~C#注册表情缘
- C#时间日期格式大全
- 【快速排序算法-C#实现】
- csharp:Nhibernate Procedure with CreateSQLQuery and GetNamedQuery
- c#中ref与out的区别
- C#的命名管道(named pipe)
- C# TabControl控件,隐藏选项卡
- C#设置word段落首行缩进为0
- double.ToString() 与 "NaN" 比较的问题
- C#中Directory 类的用法
- C#程序中利用正则表达式对字符串分组并获取组值
- C# 7.0 新特性4: 返回引用
- C#异常处理机制
- C#对泛型实例化对像的方法
- C# FTP操作类