• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c#6.0学习笔记

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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";

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
excelc#输出发布时间:2022-07-13
下一篇:
基于TestStand和C#开发平台TTStand初识系列之一【介绍】发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap