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 ...
随机推荐
- IOS UI-键盘处理和UIToolbar
// // ViewController.m // IOS_0225-键盘处理和UIToolBar // // Created by ma c on 16/2/25. // Copyright © 2 ...
- Java环境搭建---(基础)
首先下载eclipse开发工具,下载地址:http://www.eclipse.org/downloads/,界面如下: 选择eclipse juno(4.2)的版本进入界面 点击Downloads, ...
- 012PHP基础知识——运算符(五)
<?php /** * 运算符的短路: * && 逻辑与 || 逻辑或 存在短路: */ /* $a = 1; $a==1 ||$c=100; //逻辑或:第一个表达式返回tru ...
- 【hdu1005】Number Sequence
题目描述 一个数列的定义如下: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A和B,你要求出f(n). 输入 输 ...
- hystrix -hystrix常用配置介绍
配置官网介绍地址:https://github.com/Netflix/Hystrix/wiki/Configuration hystrix.command.default.execution.iso ...
- CMDB后台管理(AutoServer)
1.表结构设计 from django.db import models class UserProfile(models.Model): """ 用户信息 " ...
- compass框架的sprite雪碧图的用法简要
---恢复内容开始--- **简介** CSS SPRITE 即 CSS雪碧,即是将诸多图片合成一张图片,然后使用CSS 的background和background-position属性渲染. 这样 ...
- Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
1.加入DataSourceTransactionManager的命名空间 修改applicationContext.xml文件,增加如下内容: 1 2 3 4 5 6 7 8 9 10 11 12 ...
- LaText中插入带上下限的求和符号
效果如下: LaTex命令如下: \begin{equation} \label{8} z_{i}(k+1)=\sum_{j\in N_{i}(k)} a_{ij}(k)z_{i}(k),z_{i}( ...
- 将glassfish 添加到服务中 ,自启
将glassfish 添加到服务中 ,自启. 命令: sc create wuziServer binPath= D:\wuzi\wuzi-start.bat start= auto