c# 6.0 学习笔记
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";
c# 6.0 学习笔记的更多相关文章
- DirectX 总结和DirectX 9.0 学习笔记
转自:http://www.cnblogs.com/graphics/archive/2009/11/25/1583682.html DirectX 总结 DDS DirectXDraw Surfac ...
- 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移
不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...
- vue2.0学习笔记之路由(二)路由嵌套+动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue2.0学习笔记之路由(二)路由嵌套
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- hdcms v5.7.0学习笔记
hdcms v5.7.0学习笔记 https://note.youdao.com/ynoteshare1/index.html?id=c404d63ac910eb15a440452f73d6a6db& ...
- dhtmlxgrid v3.0学习笔记
dhtmlxgrid v3.0学习笔记 分类: dhtmlx JavaScript2012-01-31 15:41 1744人阅读 评论(0) 收藏 举报 stylesheetdatecalendar ...
- OAuth 2.0学习笔记
文章目录 OAuth的作用就是让"客户端"安全可控地获取"用户"的授权,与"服务商提供商"进行互动. OAuth在"客户端&quo ...
- 一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx、supervisor、mysql环境搭建
作为.neter,看到.net core 2.0的正式发布,心里是有点小激动的,迫不及待的体验了一把,发现速度确实是快了很多,其中也遇到一些小问题,所以整理了一些学习笔记: 阅读目录 环境说明 安装C ...
- RxJava2.0学习笔记2 2018年7月3日 周二
摘记: 1.map -- 转换 有些服务端的接口设计,会在返回的数据外层包裹一些额外信息,这些信息对于调试很有用,但本地显示是用不到的.使用 map() 可以把外层的格式剥掉,只留下本地会用到的核心 ...
- thinkphp5.0学习笔记
2019-11-11学习笔记 安装TP5.0 a)源代码包下载 在thinkphp官网下载(www.thinkphp.cn)下载 完整版本的TP5.0 b) composer 安装 切换到网站的根目录 ...
随机推荐
- [LeetCode] 219. Contains Duplicate II 解题思路
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- .NET框架设计—常被忽视的框架设计技巧
阅读目录: 1.开篇介绍 2.元数据缓存池模式(在运行时构造元数据缓存池) 2.1.元数据设计模式(抽象出对数据的描述数据) 2.2.借助Dynamic来改变IOC.AOP动态绑定的问题 2.3.元数 ...
- Flask 中的 SQLAlchemy 使用教程
Flask 是一个 python web micro framework.所谓微框架,主要是 flask 简洁与轻巧,自定义程度高.相比 django 更加轻量级. 之前一直折腾 django,得益于 ...
- WIN8.1 PROBLEMS 01
win8装好后右下角显示secureboot未正确配置桌面会显示“Windows 8.1 Secure Boot未正确配置”的水印问题: 安全启动(Secure Boot)可以阻止未授权软件的运行,提 ...
- 1001 Sum Problem [ACM刷题]
这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程. github传送门:https://github.com/haoyuanliu/Online_Judge/tree/mas ...
- swift - use backslash to add the value in the string
There’s an even simpler way to include values in strings: Write the value in parentheses, and write ...
- hdu5032 Always Cook Mushroom
题意是这样,给定一个1000x1000的点阵.m组询问.每次询问一个由(0,0).(x,0)点一以及从原点出发的方向向量(a,b)构成的直角三角形包围的点的权值和. 点的权值是(x+A)(y+B),当 ...
- LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Spring事务管理使用
发现问题 最近,碰到一个问题,再用spring实现事务管理的时候,发现不起作用,在出异常时,并不会回滚数据库操作. 我想实现的功能如下: @Transactional(isolation=Isolat ...
- [AngularJS] Design Pattern: Simple Mediator
We're going to use rootScope emit here to send out events and then we're going to listen for them in ...