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 ...
随机推荐
- c# Middleware impl
using NUnit.Framework; using System; using System.Collections.Generic; using System.Linq; using Syst ...
- java程序设计基础篇 复习笔记 第七单元&&第八单元
7.1 int[][] triArray{ {1}, {1,2}, {1,2,3}, }; 7.2 array[2].length 8.1 Unified Modeling Language:UML ...
- log4cpp之Appender
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- vim学习相关链接
1:http://blog.csdn.net/niushuai666/article/details/7275406 2:http://ju.outofmemory.cn/entry/79671 3. ...
- 概念:GNU构建系统和Autotool
经常使用Linux的开发人员或者运维人员,可能对configure->make->make install相当熟悉.事实上,这叫GNU构建系统,利用脚本和make程序在特定平台上构建软件. ...
- CentOS 7.4搭建Kubernetes 1.8.5集群
环境介绍 角色 操作系统 IP 主机名 Docker版本 master,node CentOS 7.4 192.168.0.210 node210 17.11.0-ce node CentOS 7.4 ...
- 一步步搭建自己的web服务器
IIS或者其他Web服务器究竟做了哪些工作,让浏览器请求一个URL地址后显示一个漂亮的网页?要想弄清这个疑问,我想我们可以自己写一个简单的web服务器. 思路: 创建socket监听浏览器请求. 连接 ...
- HDU2181 哈密顿绕行世界问题
解题思路:哈密顿环游世界问题.一道简单的题目,用回溯. #include<cstdio> #include<cstring> #include<algorithm> ...
- 单独编译某个pas文件
默认的询问.提示.警告框都是英文,找到Vcl.consts.pas改了下,重新编译,放到安装目录下,替换原有的即可. 1. 使用dcc32.exe编译指定的pas文件,dcc32.exe所在目录见下图 ...
- [译]SSL/TLS真的被BEAST攻击攻破了吗?真实情况是怎样的?我应该做什么?
原文链接:https://luxsci.com/blog/is-ssltls-really-broken-by-the-beast-attack-what-is-the-real-story-what ...