码迷,mamicode.com
首页 > Windows程序 > 详细

What's new in C# 7.0

时间:2019-05-11 09:18:25      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:详细信息   relative   type   元组   语言   方法   tin   值类型   ant   

1.数字分隔符(Digit Separators )

数字分隔符使代码更具可读性。在声明变量时,可以将_添加到单独的数字中。编译器只删除_。以下代码片段在C#7中看起来更具可读性:

In C# 6

long n1 = 0x1234567890ABCDEF; 

In C# 7

long n2 = 0x1234_5678_90AB_CDEF;

In C# 7.2

long n2 = 0x_1234_5678_90AB_CDEF;

2.二进制(Binary Literals )

C#7为二进制文件提供了一个新的文字。二进制文件只能包含va 0和1。现在,数字分隔符变得尤为重要

In C# 7

uint binary1 = 0b1111_0000_1010_0101_1111_0000_1010_0101;

3.Expression-bodied 函数成员(Expression-Bodied Members )

C#6允许表达式方法和属性。使用C#7,表达式主体可以与构造函数,析构函数,本地函数,属性访问器等一起使用。在这里,您可以看到C#6和C#7之间的属性访问器的区别:

In C# 6

private string _firstName;
public string FirstName
{
    get { return _firstName; }
    set { Set(ref _firstName, value); }
}

In C# 7

private string _firstName;
public string FirstName
{
    get => _firstName; 
    set => Set(ref _firstName, value);
}

4.Out 变量(Out Var)

在C#7之前,必须在使用之前声明out变量。使用C#7,代码减少了一行,因为变量可以声明

使用:

In C# 6

string n = "42";
int result;
if (string.TryParse(n, out result)
{
    Console.WriteLine($"Converting to a number was successful:{result}");
}

 

In C# 7

string n = "42";
if (string.TryParse(n, out var result)
{
    Console.WriteLine($"Converting to a number was successful:{result}");
}

 

5.非尾随命名参数(Non-Trailing Named Arguments )

使用C#7.2,支持非尾随命名参数。有关详细信息,请参阅命名参数和可选参数
In C# 7.0

if (Enum.TryParse(weekdayRecommendation.Entity, ignoreCase:true,result: out DayOfWeek weekday))

{
  reservation.Weekday = weekday;
}

In C# 7.2

 

if (Enum.TryParse(weekdayRecommendation.Entity, ignoreCase:true,out DayOfWeek weekday))
{
  reservation.Weekday = weekday; 
}

6.只读结构(Readonly Struct )

结构应该是只读的(有一些例外)。使用C#7.2,可以使用readonly修饰符声明结构,因此编译器会验证结构是否未更改。编译器也可以使用此保证不复制将其作为参数传递的结构,而是将其作为引用传递:

In C# 7.2

public readonly struct Dimensions
{
public double Length { get; }
public double Width { get; }
public Dimensions(double length, double width)
{
Length = length;
Width = width;
}
public double Diagonal => Math.Sqrt(Length * Length + Width
* Width);
}

7.In 修饰参数(In Parameters )

C#7.2还允许带有参数的in修饰符。这可以保证传递的值类型不会改变,它可以通过引用传递以避免副本:

In C# 7.2

static void CantChange(in AStruct s)
{
  // s can‘t change
} 

8.private protected 访问修饰符

C#7.2添加了一个新的访问修饰符:private protected。访问修饰符protected internal允许访问该成员(如果已使用)来自同一程序集中的类型,或来自派生自该类的另一个程序集中的类型。对于private protected,如果类派生自基类并且在同一个程序集中,则只允许使用AND而不是OR访问。

 

使用C#7.1,定义了一个默认文字,与默认运算符相比,它允许更短的语法。默认运算符总是需要重复类型,现在不再需要了。

In C# 7.0

int x = default(int);
ImmutableArray<int> arr = default(ImmutableArray<int>);

In C# 7.1

int x = default;
ImmutableArray<int> arr = default;

10.本地函数(Local Functions )

在C#7之前,无法在方法中声明函数。你可以创建一个lambda表达式并调用它,如C#6代码片段所示:

In C# 6

public void SomeFunStuff()
{
  Func<int, int, int> add = (x, y) => x + y;
  int result = add(38, 4);
  Console.WriteLine(result);
}

使用C#7,可以在方法中声明本地函数。只能在方法范围内访问本地函数:

In C# 7

public void SomeFunStuff()
{
  int add(int x, int y) => x + y;
  int result = add(38, 4);
  Console.WriteLine(result);
}

11.元组(Tuples )

低于 C# 7.0 的版本中也提供元组,但它们效率低下且不具有语言支持。 这意味着元组元素只能作为 Item1 和 Item2 等引用。C# 7.0 引入了对元组的语言支持,可利用更有效的新元组类型向元组字段赋予语义名称。在C#7中,元组是语言的一部分,你可以定义成员的名字:

in C# 6

var t1 = Tuple.Create(42, "astring"); 
int i1 = t1.Item1;
string s1 = t1.Item2;

In C# 7

var t1 = (n: 42, s: "magic");
int i1 = t1.n;
string s1 = t1.s;

12.推断元组元素名称(Inferred Tuple Names )

此功能是对 C# 7.0 中引入的元组功能一次小型增强。 在初始化元组时,许多时候,赋值操作右侧的变量名与用于元组元素的名称相同,元组元素的名称可通过在 C# 7.1 中初始化元组时使用的变量进行推断: 

In C# 7.0

int count = 5;
string label = "Colors used in the map";
var pair = (count: count, label: label);

In C# 7.1

int count = 5;
string label = "Colors used in the map";
var pair = (count, label); // element names are "count" and "label"

13.析构元组(Deconstructors )

在某些时候,你可能想要解包从方法返回的元组的成员。 可通过为元组中的每个值声明单独的变量来实现此目的。 这种解包操作称为析构元组

In C# 7

var p1 = new Person("Tom", "Turbo");
(string firstName, string lastName) = p1;

14.模式匹配(Pattern Matching )

通过模式匹配,is运算符和switch语句增强了三种模式:const模式,类型模式和var模式。以下代码段显示

使用is运算符的模式。匹配的第一次检查与常量42匹配,第二次匹配检查Person对象,第三次匹配检查具有var模式的每个对象。使用类型和var模式,可以为强类型访问声明变量:

In C# 7

public void PatternMatchingWithIsOperator(object o)
{
  if (o is 42)
  {
  }
  if (o is Person p)
  {
  }
  if (o is var v1)
  {
  }
}

使用switch语句,可以对case子句使用相同的模式。如果模式匹配,您还可以声明要强类型的变量。您还可以使用何时在条件上过滤模式:

public void PatternMatchingWithSwitchStatement(object o)
{
  swtich (o)
  {
    case 42:
    break; 
    case Person p when p.FirstName == "Katharina":
    break;
    case Person p:
    break;
    case var v:
    break;
  }
}

注:使用 when 关键字来指定模式的其他规则。

15.Throw表达式(Throw Expressions )

抛出异常只能通过声明来实现;在表达中是不可能的。因此,当使用构造函数接收参数时,需要额外检查null以抛出ArgumentNullException。使用C#7,可以在表达式中抛出异常,因此当左侧为空时,可以使用合并运算符抛出ArgumentNullException

In C# 6

private readonly IBooksService _booksService;
public BookController(BooksService booksService)
{
  if (booksService == null)
  {
    throw new ArgumentNullException(nameof(b));
  }
  _booksService = booksService;
}

In C# 7

private readonly IBooksService _booksService;
public BookController(BooksService booksService)
{
  _booksService = booksService ?? throw new ArgumentNullException(nameof(b));
}

16.异步Main方法

在C#7.1之前,Main方法总是需要声明为void类型。用C#7。1,Main方法也可以是Task类型,并使用async和await关键字:
In C# 7.0

static void Main()
{
  SomeMethodAsync().Wait();
}

In C# 7.1

async static Task Main()
{
  await SomeMethodAsync();
}

17.条件Ref表达式

以下代码片段声明方法GetNumber以返回对int的引用。这条路,调用者可以直接访问数组中的元素,并可以更改其内容:

In C# 7.0

int[] _numbers = { 3, 7, 11, 15, 21 };
public ref int GetNumber(int index)
{
return ref _numbers[index];
} 

In C# 7.2

变量 r 是对 arr 或 otherArr 中第一个值的引用。

ref var r = ref (arr != null ? ref arr[0] : ref otherArr[0]);

扩展:

1.使用C#7.2,可以将readonly修饰符添加到ref返回。调用者接收引用但不允许更改它:

In C# 7.2

int[] _numbers = { 3, 7, 11, 15, 21 };
public ref readonly int GetNumber(int index)
{
    return ref _numbers[index];
}

2.ref Struct (在C#7.2之前,C#可以创建引用类型(类)和值类型(结构)。但是,当发生装箱时,结构也可以存储在堆上。用C#7.2,可以声明只允许在堆栈上使用的类型:ref struct)

In C# 7.2

ref struct OnlyOnTheStack
{
} 

C# 更多语法糖请参阅微软官方文档.https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/index

What's new in C# 7.0

标签:详细信息   relative   type   元组   语言   方法   tin   值类型   ant   

原文地址:https://www.cnblogs.com/yhqclub/p/10847565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!