本文为转载,学习研究

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. 基于css3的环形动态进度条(原创)

    基于css3实现的环形动态加载条,也用到了jquery.当时的想法是通过两个半圆的转动,来实现相应的效果,其实用css3的animation也可以实现这种效果.之所以用jquery是因为通过jquer ...

  2. Lua学习系列(三)

    Ubuntu14.04 上源码编译安装lua5.3 原文:http://blog.csdn.net/abclixu123/article/details/46676991

  3. (简单) HDU 2612 Find a way,BFS。

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  4. android中广播的使用

    广播消息机制用于进行系统级别的消息通知,每个应用程序可以对感兴趣的广播进行注册,并且将接收广播的方法定义在广播接收器中(Broadcast). 广播可以分为标准广播和有序广播. 注册广播的方法可以动态 ...

  5. Java的内存泄漏

    内存泄漏是指,无用对象(不再使用的对象)持续占用内存或者无用对象的内存得不到及时释放,从而造成的内存浪费 就说是有一块内存你不需要再用了,但是呢你还保留着它的指针,那么这块内存就不会被回收 举个例子 ...

  6. 微信小程序之----audio音频播放

    audio audio为音频组件,我们可以轻松的在小程序中播放音频. audio组件属性如下: 属性名 类型 默认值 说明 id String   video 组件的唯一标识符, src String ...

  7. leetcode day5

    [242]Valid Anagram: Given two strings s and t, write a function to determine if t is an anagram of s ...

  8. onethink微博插件雏形记

    2014年7月30日 17:08:44 后台微博插件: 一.功能: 1.绑定微博 2.发布的文章自动发布到新浪微博 3.插件独立性强,修改地方少 二.效果: 插件目录 工程地址:http://down ...

  9. 安卓selector

    定义styles.xml <?xml version="1.0" encoding="utf-8"?> <resources> < ...

  10. GP项目总结(一)

    1.使用activity渲染不同的View时,两种方法: (1.)自定义两个不同的View,然后在mainActivity里根据不同的数据使用不同的View,通过addView()来Activity里 ...