C# 6.0 新特性收集
public JObject ToJson()
{
var result = new JObject();
result["X"] = X;
result["Y"] = Y;
return result;
}
改进后的代码可以这么写

public JObject ToJson()
{
var result = new JObject()
{
["X"] = X,
["Y"] = Y
};
return result;
}
最终可以化简成一行代码
public JObject ToJson() => new JObject() { ["X"] = X, ["Y"] = Y };
1. 静态using(static using)
静态using声明允许不使用类名直接调用静态方法
In C# 5
public bool IsSquare(Rectangle rect)
{
return rect.Height == rect.Width;
}
In C# 6
public bool IsSquare(Rectangle rect) => rect.Height == rect.Width;
1. 方法(Methods)
1 public Student Create() => new Student();
等同于:
1 public Student Create()
2 {
3returnnew Student();
4 }
2. 只读属性(read only properties)
1 publicstring FullName => string.Format("{0},{1}", FirstName, LastName);
等同于:
1 publicstring FullName
2 {
3get4 {
5returnstring.Format("{0},{1}", FirstName, LastName);
6 }
7 }
原理解析:上面的表达式在编译后会生成最原始的方法体和访问器,值得一提的是函数表达式体跟Lambda是两个相似但不相同的东西,函数的表
达式体只能带一句话且不能包含return关键字但Lambda 能带语句块和包含关键字。
public Point Move(int dx, int dy) => new Point(x + dx, y + dy);
再来举一个简单的例子:一个没有返回值的函数
publicvoid Print() => Console.WriteLine(FirstName + " " + LastName);
In C# 5
publicclassPerson
{
publicPerson()
{
Age = 24;
}
publicint Age {get; set;}
}
In C# 6
publicclassPerson
{
publicint Age {get; set;} = 42;
}
In C# 5
privatereadonlyint _bookId;
public BookId
{
get
{
return _bookId;
}
}
In C# 6
publicBookId {get;}
In C# 5
publicvoidMethod(object o)
{
if (o == null) thrownew ArgumentNullException("o");
}
In C# 6
publicvoidMethod(object o)
{
if (o == null) thrownew ArgumentNullException(nameof(o));
}
- public class MyClass
- {
- [TestMethod]
- public static void Show(int age)
- {
- Console.WriteLine(nameof(MyClass)); // 输出 MyClass 类名
- Console.WriteLine(nameof(Show)); // 输出 Show 方法名
- Console.WriteLine(nameof(age)); // 输出 age
- Console.WriteLine(nameof(TestMethodAttribute)) // 输出 Attribute 名
- }
- }
int? age = p == null ? null : p.Age;
var handler = Event;
if (handler != null)
{
handler(source, e);
}
In C# 6
int? age = p?.Age;
handler?.Invoke(source, e);
In C# 5
var dict = new Dictionary<int, string>();
dict.Add(3,"three");
dict.Add(7,"seven");
In C# 6
var dict = new Dictionary<int, string>()
{
[3] ="three",
[7] ="seven"
};
In C# 5
try
{
//etc.
} catch (MyException ex)
{
if (ex.ErrorCode != 405) throw;
// etc.
}
In C# 6
try
{
//etc.
} catch (MyException ex) when (ex.ErrorCode == 405)
{
// etc.
}
bool hasError = false;
string errorMessage = null;
try
{
//etc.
} catch (MyException ex)
{
hasError = true;
errorMessage = ex.Message;
}
if (hasError)
{
awaitnew MessageDialog().ShowAsync(errorMessage);
}
In C# 6
try
{
//etc.
} catch (MyException ex)
{
awaitnew MessageDialog().ShowAsync(ex.Message);
}



C# 6.0 新特性收集的更多相关文章
- C# 7.0 新特性收集
1.out-variables(Out变量) 2.Tuples(元组) 3.Pattern Matching(匹配模式) 4.ref locals and returns (局部变量和引用返回) 5. ...
- C#7.0&6.0新特性 — 完整版
C#2.0 泛型 部分类型 匿名方法 迭代器 可空类型 Getter / setter单独可访问性 方法组转换(代表) Co- and Contra-variance for delegates 静态 ...
- Atitit. C#.net clr 2.0 4.0新特性
Atitit. C#.net clr 2.0 4.0新特性 1. CLR内部结构1 2. CLR 版本发展史3 3. CLR 2.0 3 4. CLR 4 新特性 概览4 4.1.1. 托管与本地 ...
- MySQL 8.0 新特性梳理汇总
一 历史版本发布回顾 从上图可以看出,基本遵循 5+3+3 模式 5---GA发布后,5年 就停止通用常规的更新了(功能不再更新了): 3---企业版的,+3年功能不再更新了: 3 ---完全停止更新 ...
- 浅谈Tuple之C#4.0新特性那些事儿你还记得多少?
来源:微信公众号CodeL 今天给大家分享的内容基于前几天收到的一条留言信息,留言内容是这样的: 看了这位网友的留言相信有不少刚接触开发的童鞋们也会有同样的困惑,除了用新建类作为桥梁之外还有什么好的办 ...
- Java基础和JDK5.0新特性
Java基础 JDK5.0新特性 PS: JDK:Java Development KitsJRE: Java Runtime EvironmentJRE = JVM + ClassLibary JV ...
- Visual Studio 2015速递(1)——C#6.0新特性怎么用
系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...
- atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性
atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性 1.1. Servlet和JSP规范版本对应关系:1 1.2. Servlet2 ...
- 背水一战 Windows 10 (1) - C# 6.0 新特性
[源码下载] 背水一战 Windows 10 (1) - C# 6.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 6.0 新特性 介绍 C# 6.0 的新特性 示例1 ...
随机推荐
- 031——VUE中表单控件处理之使用vue控制input和textarea表单项
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java程序设计基础篇 复习笔记 第二单元
1原始数据类型(primitive data type) == 基本类型 (fundamental type)byte short int long float double char boolean ...
- 在微信里面打开链接,显示501 Not Implemented,但是同样的链接在其他浏览器是可以打开的。
在微信里面打开链接,显示501 Not Implemented,但是同样的链接在其他浏览器是可以打开的. 显示: 还原:该链接在2017年之前微信还是可以访问的. 访问的地址格式是:http://xx ...
- jmeter的三种参数化
以FTP请求(用户.密码)为例:(其他都相同) 1.文件参数化 使用配置元件中的CSV Data Set Config 配置CSV Data Set Config: 文件中存储ftp登录的用户名和密码 ...
- Python的集合框架
Python内置了四种集合框架(list.tuple.dict.set) list:list是一种有序的集合 list里面的元素的数据类型也可以不同,list元素也可以是另一个list In [28] ...
- C++11_ Lambda
版权声明:本文为博主原创文章,未经博主允许不得转载. 这次主要介绍C++11的Lambda语法,一个非常给力的语法 1.组成 : [...导入符号](...参数)mutable(可改写) throw ...
- 2018-2019-2 网络对抗技术 20165210 Exp4 恶意代码分析
2018-2019-2 网络对抗技术 20165210 Exp4 恶意代码分析 一.实验目标 首先是监控你自己系统的运行状态,看有没有可疑的程序在运行. 其次是分析一个恶意软件,就分析Exp2或Exp ...
- 手机通过笔记本开的WIFI访问TOMCAT服务器站点示例
我一直想用手机连上笔记本上的服务器TOMCAT,尝试了好久没连上,实验室一个妹子会这个技术,我也想学,自己摸索着学了几次,没成功,今晚想个办法试了一下,可以连接了,以后可以做手机网站开发了,这也是移动 ...
- HAWQ + MADlib 玩转数据挖掘之(二)——矩阵
矩阵是Madlib中数据的基本格式,通常是二维的.在Madlib中,数组的概念与向量类似,数组通常是一维的,是矩阵的一种特殊形式. 一.矩阵表示 MADlib为矩阵提供了两种表示形式:稠密和稀疏. 1 ...
- chrome浏览器使用记录
出现错误 net::ERR_BLOCKED_BY_CLIENT 出现这个错误一般是因为chrome安装了adblocker等这样的插件,这些插件会把路径及文件名中包含广告字样的文字禁止掉,比如:adv ...