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 ...
随机推荐
- Python之路,Day8 - 面向对象编程进阶
本节内容: 面向对象高级语法部分 经典类vs新式类 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 经典 ...
- 让你的ansible飞起来
一.SSH Multiplexing 1 配置 vim /etc/ssh/ssh_config Host * GSSAPIAuthentication yes # If this option is ...
- 记一次GreenPlum性能调优
在部署了的GreenPlum集群中进行数据查询时,发现数据量一旦大了,查询一跑就中断,提示某个segment中断了连接. ERROR 58M01 "Error on receive from ...
- 《gradle 用户指南中文版》目录
gradle 用户指南 版权所有©2007-2017 Hans Dockter,Adam Murdoch只要您不对这些副本收取任何费用,并且进一步规定,每个副本都包含本版权声明,无论是以印刷版还是电子 ...
- Shell 命令行批量处理图片文件名
Shell 命令行批量处理图片文件名 从网上下载了一堆图片,有的是*.jpg的,有的是*.jpeg的.并且文件名有长有短,很是糟心.因此,我想把这些文件给全部整理好,当然是用shell来处理啦! 说干 ...
- flowable FormEngine和FormEngineConfiguration
FormEngineConfiguration 继承自 AbstractEngineConfiguration. 一.获得实例 FormEngineConfiguration提供了7个公开的静态方法: ...
- angular 中不要使用location.href
location.href = '#/HKorderList?gid='+gid+'&gname='+encodeURIComponent(gname)+'&cPeriod='+$(' ...
- HTML中Div、span、label标签的区别
div与span 大家在初学div+css布局时,有很多困惑,在div与span的使用过程没觉得有一定的”章法”,觉得两个区别不大,在w3c的关于div和span的定义:div作为分割文档结构自然使它 ...
- 【剑指offer】06从尾到头打印链表,C++实现
本文是原创文章,转载请注明出处! 0.前言 # 本文为牛客网<剑指offer>刷题笔记 1.题目 # 输入一个链表,从尾到头打印链表每个节点的值 2.思路 # 不改变链表结构的情况下,首先 ...
- web.xml配置详解(2)
1 定义头和根元素 部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出文件的字符编码.DOCYTPE声明必须立即出现在此头之后.这个声明告诉服务器适用的 ...