Professional C# 6 and .NET Core 1.0 - What’s New in C# 6
本文为转载,学习研究
What’s New in C# 6
With C# 6 a new C# compiler is available. It’s not only that a source codecleanup
was done; the features of the compiler pipeline can now be used from custom
programs, and are used by many features of Visual Studio.
This new compiler platform made it possible to enhance C# with many new
features. Although there’s not a feature with such an impact as LINQ orthe async
keyword, the many enhancements increase developer productivity. What arethe
changes of C# 6?
static using
The static using declarationallows invoking static methods without the class
name:
In C# 5
using System;
// etc.
Console.WriteLine("Hello,World!");
In C# 6
using static System.Console;
// etc.
WriteLine("Hello,World");
The using static keyword iscovered in Chapter 2, “Core C#.”
Expression-Bodied Methods
With expression-bodied methods, a method that includes just one statementcan
be written with the lambda syntax:
In C# 5
public boolIsSquare(Rectangle rect)
{
return rect.Height == rect.Width;
}
In C# 6
public boolIsSquare(Rectangle rect) => rect.Height == rect.Width;
Expression-bodied methods are covered in Chapter 3, “Objects and Types.”
Expression-Bodied Properties
Similar to expression-bodied methods, one-line properties with only a getaccessor
can be written with the lambda syntax:
In C# 5
public string FullName
{
get
{
return FirstName +"" + LastName;
}
}
In C# 6
public string FullName=> FirstName +"" + LastName;
Expression-bodied properties are covered in Chapter 3.
Auto-Implemented PropertyIntializers
Auto-implemented properties can be initialized with a propertyinitializer:
In C# 5
public class Person
{
public Person()
{
Age = 24;
}
public int Age {get; set;}
}
In C# 6
public class Person
{
public int Age {get; set;} = 42;
}
Auto-implemented property initializers are covered in Chapter 3.
Read-Only Auto Properties
To implement read-only properties, C# 5 requires the full property syntax.With
C# 6, you can do this using auto-implemented properties:
In C# 5
private readonly int_bookId;
public BookId
{
get
{
return _bookId;
}
}
In C# 6
public BookId {get;}
Read-only auto properties are covered in Chapter 3.
nameof Operator
With the new nameof operator, namesof fields, properties, methods, or types can
be accessed. With this, name changes are not missed with refactoring:
In C# 5
public void Method(objecto)
{
if (o == null) throw newArgumentNullException("o");
In C# 6
public void Method(objecto)
{
if (o == null) throw newArgumentNullException(nameof(o));
The nameof operator iscovered in Chapter 8, “Operators and Casts.”
Null Propagation Operator
The null propagation operator simplifies null checks:
In C# 5
int? age = p == null ?null : p.Age;
In C# 6
int? age = p?.Age;
The new syntax also has an advantage for firing events:
In C# 5
var handler = Event;
if (handler != null)
{
handler(source, e);
}
In C# 6
handler?.Invoke(source,e);
The null propagation operator is covered in Chapter 8.
String Interpolation
The string interpolation removes calls to string.Format. Instead of using
numbered format placeholders in the string, the placeholders can include
expressions:
In C# 5
public override ToString()
{
return string.Format("{0}, {1}",Title, Publisher);
}
In C# 6
public override ToString()=> $"{Title} {Publisher}";
The C# 6 sample is reduced that much compared to the C# 5 syntax becauseit
uses not only string interpolation but also an expression-bodied method.
String interpolation can also use string formats and get special featureson
assigning it to a FormattableString. Stringinterpolation is covered in Chapter 10,
“Strings and Regular Expressions.”
Dictionary Initializers
Dictionaries can now be initialized with a dictionary initializer—similarto the
collection initializer.
In C# 5
var dict = newDictionary<int, string>();
dict.Add(3,"three");
dict.Add(7,"seven");
In C# 6
var dict = newDictionary<int, string>()
{
[3] ="three",
[7] ="seven"
};
Dictionary initializers are covered in Chapter 11, “Collections.”
Exception Filters
Exception filters allow you to filter exceptions before catching them.
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.
}
A big advantage of the new syntax is not only that it reduces the codelength but
also that the stack trace is not changed—which happens with the C# 5variant.
Exception filters are covered in Chapter 14, “Errors and Exceptions.”
Await in Catch
await can now be usedin the catch clause. C# 5required a workaround.
In C# 5
bool hasError = false;
string errorMessage =null;
try
{
//etc.
}
catch (MyException ex)
{
hasError = true;
errorMessage = ex.Message;
}
if (hasError)
{
await newMessageDialog().ShowAsync(errorMessage);
}
In C# 6
try
{
//etc.
}
catch (MyException ex)
{
await newMessageDialog().ShowAsync(ex.Message);
}
This feature doesn’t need an enhancement of the C# syntax; it’sfunctionality
that’s working now. This enhancement required a lot of investment from
Microsoft to make it work, but that really doesn’t matter to you usingthis
platform. For you, it means less code is needed—just compare the twoversions.
NOTE The new C# 6 language features are covered in the mentioned
chapters, and in all chapters of this book the new C# syntax isused.
Professional C# 6 and .NET Core 1.0 - What’s New in C# 6的更多相关文章
- Professional C# 6 and .NET Core 1.0 - 37 ADO.NET
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 37 ADO.NET -------- ...
- Professional C# 6 and .NET Core 1.0 - 38 Entity Framework Core
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 38 Entity Framework ...
- Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Services
本文内容为转载,供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Servi ...
- Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core --- ...
- Professional C# 6 and .NET Core 1.0 - Chapter 43 WebHooks and SignalR
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 43 WebHooks ...
- Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity Framework Core
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity F ...
- Professional C# 6 and .NET Core 1.0 - Chapter 37 ADO.NET
本文内容为转载,供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 37 ADO.NET 译文:C# 6 与 .NE ...
- Professional C# 6 and .NET Core 1.0 - Chapter 41 ASP.NET MVC
What's In This Chapter? Features of ASP.NET MVC 6 Routing Creating Controllers Creating Views Valida ...
- Professional C# 6 and .NET Core 1.0 - Chapter 42 ASP.NET Web API
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处: -------------------------------------------------------- ...
- Professional C# 6 and .NET Core 1.0 - Creating Hello, World! with Visual Studio
本文为转载,学习研究 Creating Hello, World! with Visual Studio Chapter 1, “.NET Application Architectures,” ex ...
随机推荐
- ACM录 之 常识和错误。
接下来说说一些ACM里面的常识和错误...(可能会比较乱) —— 首先ACM里面的代码都是要提交上去,然后让计算机自动判题的,所以...千万不要把 system("pause"); ...
- spring3mvc与struts2比较
目前企业中使用SpringMvc的比例已经远远超过Struts2,那么两者到底有什么区别,是很多初学者比较关注的问题,下面我们就来对SpringMvc和Struts2进行各方面的比较: 1. 核 心控 ...
- artTemplate-3.0(与项目实际结合)
引入artTemplate.js <script type="text/javascript" src="${ctx}/assets/plugins/artTemp ...
- P3197 [HNOI2008]越狱
题目描述 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 输入输出格式 输入 ...
- 让程序同时只能运行一个C++ Builder实现(转)
源:让程序同时只能运行一个 很多人都讨论过这个问题, 这里用Victor串口控件里面现成的共享内存功能来实现. 当程序运行第二次时只是激活第一次运行的窗口, 而不是再运行一个程序. 需要在主程序里实现 ...
- stm32驱动DS1302芯片
天时可以自动调整,且具有闰年补偿功能.工作电压宽达2.5-5.5V.采用双电源供电(主电源和备用电源),可设置备用电源充电方式,提供了对后背电源进行涓细电流充电的能力.DS1302的外部引脚分配如下图 ...
- OpenGL ES
前言 OpenGL ES是Khronos Group创建的一系列API中的一种(官方组织是:http://www.khronos.org/).在桌面计算机上有两套标准的 3DAPI:Direct3D和 ...
- 【转】国外程序员整理的 C++ 资源大全
内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了STL容器,算法和函数等. C++ Standard Library:是一系列类 ...
- 多线程的并发问题,lock用法
开启多个线程,每个线程中多次操作公共变量 using System; using System.Collections.Generic; using System.Linq; using System ...
- CSS实现背景透明而背景上的文字图片不透明
1.用图片则能兼容IE8和IE7 2.用颜色则不能兼容IE8和IE7,并且颜色层不能随着内容层自增长,只能设置一个固定高度 3.用颜色则颜色层不能包含内容层(图片和文字) <!-- wrap最外 ...