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 ...
随机推荐
- protobuf python api
摘要: python中一切都可以看作类.那么如何查看每个类的API.使用ipython python protobuf 的函数在message中定义 此处所有的api说明:https://devel ...
- AVR编程_如何通过软件复位AVR?(转)
源:http://blog.sina.com.cn/s/blog_493520900100bpos.html Question 如何通过软件复位AVR? Answer 如果你想通过软件复位AVR,你应 ...
- js 如何动态添加数组_百度知道
1.数组的创建var arrayObj = new Array(); //创建一个数组var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长 ...
- DIV 和 SPAN 区别
DIV 和 SPAN 元素最大的特点是默认都没有对元素内的对象进行任何格式化渲染.主要用于应用样式表(共同点). 两者最明显的区别在于DIV是块元素,而SPAN是行内元素(也译作内嵌元素). 详解:1 ...
- STM32内置参照电压的使用(转)
源:STM32内置参照电压的使用 每个STM32芯片都有一个内部的参照电压,相当于一个标准电压测量点,在芯片内部连接到ADC1的通道17. 根据数据手册中的数据,这个参照电压的典型值是1.20V,最小 ...
- iOS进阶
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:wjh2005链接:https://www.zhihu.com/question/28518265/answer/887505 ...
- [bzoj1195] [hnoi2006] 最短母串
本题是一个经典的状压dp问题,在紫书中有着加强版的例题. 本题的难度主要体现在:如何输出字符串字典序最小. 为了解决这个问题,我们有两种常用方案: 1) 我们可以采用bfs输出路径的方法,使用+1来输 ...
- STM32_IAP详解(有代码,有上位机)
Iap,全名为in applacation programming,即在应用编程,与之相对应的叫做isp,in system programming,在系统编程,两者的不同是isp需要依靠烧写器在单片 ...
- Android和BLE模块连接通信
首先,进行一下科普: 1.BLE(Bluetooth Low Energy),蓝牙4.0核心profile,主要特点是快速搜索,快速连接,超低功耗保持连接和数据传输,缺点:数据传输速率低,由于其具有低 ...
- Run Loop简介 分类: ios技术 ios相关 2015-03-11 22:21 73人阅读 评论(0) 收藏
做了一年多的IOS开发,对IOS和Objective-C深层次的了解还十分有限,大多还停留在会用API的级别,这是件挺可悲的事情.想学好一门语言还是需要深层次的了解它,这样才能在使用的时候得心应手,出 ...