本文为转载,学习研究

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的更多相关文章

  1. Professional C# 6 and .NET Core 1.0 - 37 ADO.NET

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 37 ADO.NET -------- ...

  2. Professional C# 6 and .NET Core 1.0 - 38 Entity Framework Core

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 38 Entity Framework ...

  3. Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Services

    本文内容为转载,供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Servi ...

  4. Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core --- ...

  5. Professional C# 6 and .NET Core 1.0 - Chapter 43 WebHooks and SignalR

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 43 WebHooks ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Professional C# 6 and .NET Core 1.0 - Chapter 42 ASP.NET Web API

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处: -------------------------------------------------------- ...

  10. 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 ...

随机推荐

  1. SQL复习六(视图)

    视图是关系数据库系统提供给用户以多角度观察数据库中数据的一种重要方法.视图是从一个或者几个表中导出的虚拟表.视图一经定义就可以被查询和删除.也可以在视图上定义视图.用视图完成数据的更新(增,删,改)操 ...

  2. Apache Commons工具集简介(转)

    此文为转帖,原帖地址:http://zhoualine.iteye.com/blog/1770014

  3. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

    Asp.net Mvc 未能加载类型“System.Web.Mvc.ViewPage 的解決方法 2010-11-30 17:31:51|  分类: .net mvc |举报 |字号 订阅   如果多 ...

  4. 导出WAS已部署的ear包的几种方法

    可以通过下面几种办法将部署好的工程导出为一个ear包. 1.最简单的,通过was的控制台导出: 首先登录控制台,进入"企业应用程序"管理页面,选中要导出的工程,点击"导出 ...

  5. 无限循环小数POJ1930

    题意:给定一个无限循环小数,求其分数形势,要求分母最小 分析:看了别人的题解才做出来的,将无限循环小数转化成分数,分为纯循环和混循环两种形式. (1)对于纯循环:用9做分母,有多少个循环数就几个9,比 ...

  6. [Unity AssetBundle]Asset资源处理

    什么是AssetBundle 在很多类型游戏的制作过程中,开发者都会考虑一个非常重要的问题,即如何在游戏运行过程中对资源进行动态的下载和加载.因此,Unity引擎引入了AssetBundle这一技术来 ...

  7. FZU 1058 粗心的物理学家

    这题有毒.要用long double定义,以及cout控制格式输出. #include<cstdio> #include<cstring> #include<cmath& ...

  8. IOS开发中UITableView(表视图)的滚动优化及自定义Cell

    IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...

  9. Linux_System2

    1.从服务器下载http*.tar.gz源码包,安装到/usr/local/apache目录下,要求安装时指定能够动态加载模块,能够支持地址回写功能,能够使用ssl加密功能../configure — ...

  10. 获取IE浏览器关闭事件

    //关闭浏览器时才会触发此操作,刷新页面不执行 //n 检测鼠标相对于用户屏幕的水平位置 - 网页正文部分左:求出鼠标在当前窗口上的水平位置(参照:当前窗口右上角为0.0坐标) //m 网页正文全文宽 ...