Professional C# 6 and .NET Core 1.0 - What’s New in C# 6分分

作者:编程技术

7. Null传递操作符(Null Propagation Operator)

Null传递操作符简化了空值检查。

The null propagation operator simplifies null checks.

In C# 5

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);

 

4. 电动属性最早化器(Auto-Implemented Property Intializers)

自行属性能够选择品质早先化器初阶化。

Auto-implemented properties can be initialized with a property initializer.

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;
}

 

3. 表明式属性(Expression-Bodied Properties)

跟表明式方法雷同,唯有四个get采访器的单行属性能够应用lambda语法写。

Similar to expression-bodied methods, one-line properties with only a get accessor
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;

{

6. nameof操作符(nameof Operator)

字段、属性、方法和项目标name能够经过nameof访谈。使用nameof,能够低价的重构name变化。

With the new nameof operator, names of fields, properties, methods, or types can
be accessed. With this, name changes are not missed with refactoring.

In C# 5

public void Method(object o)
{
    if (o == null) throw new ArgumentNullException("o");

In C# 6

public void Method(object o)
{
    if (o == null) throw new ArgumentNullException(nameof(o));

The static using declarationallows invoking static methods without the class name:

1. 静态using(static using)

静态using证明允许不应用类名直接调用静态方法。

The static using declaration allows invoking static methods without the class
name.

In C# 5

using System;

Console.WriteLine("Hello, World!");

In C# 6

using static System.Console;

WriteLine("Hello, World");

{

9. 字典初步化器(Dictionary Initializers)

字典能够动用相同群集的字典开头化器领头化。

Dictionaries can now be initialized with a dictionary initializer—similar to the
collection initializer.

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"
};

  // etc.

10. 要命过滤器(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.
}

Exception filters allow you to filter exceptions before catching them. 

5. 只读自动属性(Read-Only Auto Properties)

C# 5要求总体的属性语法完毕只读属性,C# 6能够运用自动属性实现。

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;}

using System;

11. 在Catch使用Await(Await in Catch)

await能够在catch块中平素动用,C# 5中须要扭转使用。

await can now be used in the catch clause. C# 5 required a workaround.

In C# 5

bool hasError = false;
string errorMessage = null;
try
{
    //etc.
} catch (MyException ex)
{
    hasError = true;
    errorMessage = ex.Message;
} 
if (hasError)
{
    await new MessageDialog().ShowAsync(errorMessage);
}

In C# 6

try
{
    //etc.
} catch (MyException ex)
{
    await new MessageDialog().ShowAsync(ex.Message);
}

  errorMessage = ex.Message;

8. 字符串插值(String Interpolation)

字符串差值移除了对string.Format的调用,使用表明式占位符替代数字格式占位符。

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}";

 

2. 表明式方法(Expression-Bodied Methods)

运用表达式方法,独有一条语句的不二等秘书籍能够行使lambda语法写。

With expression-bodied methods, a method that includes just one statement can
be written with the lambda syntax.

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;

  hasError = true;

  {

}

try

{

In C# 6 

 

}

 

  public Person()

dict.Add(3,"three");

In C# 5 

 

 

{

}

{

if (handler != null)

What’s New in C# 6 

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.

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

Read-only auto properties are covered in Chapter 3.

The string interpolation removes calls to string.Format. Instead of using  numbered format placeholders in the string, the placeholders can include expressions: 

// etc.

In C# 6 

  // etc.

In C# 5 

  //etc.

 

    return _bookId;

WriteLine("Hello,World"); 

  }

  return string.Format("{0}, {1}",Title, Publisher);

 

}

Auto-implemented property initializers are covered in Chapter 3.

public string FullName=> FirstName "" LastName;

}

Dictionary Initializers 

 

public void Method(objecto)

}

public class Person

  //etc.

 

In C# 6 

 

The nameof operator iscovered in Chapter 8, “Operators and Casts.”

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.

var dict = newDictionary<int, string>();

try

The null propagation operator is covered in Chapter 8.

{

 

public class Person

 

With the new nameof operator, namesof fields, properties, methods, or types can be accessed. With this, name changes are not missed with refactoring: 

public string FullName

 

{

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. 

await can now be usedin the catch clause. C# 5required a workaround. 

{

In C# 5 

}

int? age = p?.Age;

  }

}

catch (MyException ex)

}

if (hasError)

 

{

public override ToString()=> $"{Title} {Publisher}";

{

 

In C# 5 

In C# 6 

Expression-Bodied Properties

public BookId {get;}

catch (MyException ex)

catch (MyException ex)when (ex.ErrorCode == 405)

With C# 6 a new C# compiler is available. It’s not only that a source code cleanup was done; the features of the compiler pipeline can now be used from custom programs, and are used by many features of Visual Studio. 

 

  handler(source, e);

In C# 5 

nameof Operator 

}

Await in Catch 

In C# 5 

 

{

 

  get

 

catch (MyException ex)

In C# 5 

In C# 6 

Console.WriteLine("Hello,World!");

}

 

 

In C# 5

 

  get

    Age = 24;

Exception Filters 

Auto-implemented properties can be initialized with a propertyinitializer: 

  {

Auto-Implemented PropertyIntializers 

{

  await newMessageDialog().ShowAsync(errorMessage);

  [3] ="three",

public boolIsSquare(Rectangle rect) => rect.Height == rect.Width;

}

var handler = Event;

Dictionaries can now be initialized with a dictionary initializer—similarto the

 

In C# 6 

{

 

{

public override ToString()

  public int Age {get; set;}

Read-Only Auto Properties 

With expression-bodied methods, a method that includes just one statement can be written with the lambda syntax: 

}

private readonly int_bookId;

  if (o == null) throw newArgumentNullException(nameof(o));

 

{

  await newMessageDialog().ShowAsync(ex.Message);

In C# 6 

In C# 6 

The null propagation operator simplifies null checks: 

Expression-bodied methods are covered in Chapter 3, “Objects and Types.”

The new syntax also has an advantage for firing events: 

Expression-bodied properties are covered in Chapter 3.

{

  //etc.

In C# 6 

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 are the changes of C# 6?

Dictionary initializers are covered in Chapter 11, “Collections.”

};

{

{

 

Similar to expression-bodied methods, one-line properties with only a getaccessor can be written with the lambda syntax:

To implement read-only properties, C# 5 requires the full property syntax.With C# 6, you can do this using auto-implemented properties:

  public int Age {get; set;} = 42;

try

var dict = newDictionary<int, string>()

  return rect.Height == rect.Width;

}

  }

  {

 

 

本文为转发,学习钻探

static using 

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

The using static keyword iscovered in Chapter 2, “Core C#.”

// etc.

In C# 5 

In C# 6 

 

using static System.Console;

Null Propagation Operator 

collection initializer. 

handler?.Invoke(source,e);

 In C# 5 

 

In C# 6 

In C# 5 

In C# 5 

try

 

 

public void Method(objecto)

String Interpolation 

bool hasError = false;

    return FirstName "" LastName;

}

 

  if (o == null) throw newArgumentNullException("o");

string errorMessage =null;

dict.Add(7,"seven");

int? age = p == null ?null : p.Age;

{

}

  //etc.

  [7] ="seven"

 

public boolIsSquare(Rectangle rect)

Expression-Bodied Methods 

 

public BookId

In C# 6

  if (ex.ErrorCode != 405) throw;

本文由分分快三计划发布,转载请注明来源

关键词: 分分快三计划