Home  >  Article  >  Backend Development  >  Future plans for C#

Future plans for C#

伊谢尔伦
伊谢尔伦Original
2016-11-24 13:19:451458browse

At NDC London, Mads Torgersen proposed plans for the future of the C# language. It should be noted that these are just plans and there is no guarantee of which version will be released. Damien Guard summarizes and briefly analyzes these recommendations, and we've picked out some of them for you here.

 Read-only properties

 Read-only automatic properties will allow developers to declare properties and their background fields in one line of code.

public int X { get; } = x;

 Static type using statement

  Both Visual Basic and Java allow importing modules (C# static classes) in namespaces. This removes repeated code in front of commonly used static functions, such as "Math."

 Main constructor

 By adding parameters after the class name, developers no longer need to explicitly create a constructor. This removes verbose code when copying constructor arguments to private fields.

public class Point(int x, int y)  {     private int x, y; }

 Property and method expressions

 Property expressions can eliminate some of the boilerplate code that was originally necessary for simple read-only properties.

public double Distance => Math.Sqrt((X * X) + (Y * Y));

 The same goes for method expressions, of course they can accept parameters. Note that parameterized properties are still not taken into account. For a long time in the future, this can only be a unique feature of VB.

  Function parameters

Most developers nowadays never use arrays unless they use the params keyword. Therefore, someone proposed that params also support the IEnumerable interface. If you do this, other languages ​​(such as Visual Basic) will also need to be supported. It has also been proposed that local variables can be declared using the out keyword. For example,

int.TryParse("123", out int x);

  Null propagation

When dealing with messy data, developers often need to write a series of null judgment logic before reading properties or calling methods. The ?. syntax will help us eliminate this logic, and the following method will be called only when the previous value is not empty.

var bestValue = points?.FirstOrDefault()?.X;

 In this example, if points is empty, or points.FirstOrDefault() returns empty, then .X will be ignored and empty will be returned. It can also be used in conjunction with ?? to provide optional default values.

var bestValue = points?.FirstOrDefault()?.X ?? -1;

 “Message passing” languages ​​like Objective-C and Smalltalk all contain such syntax. It is often considered problematic because a null reference exception is silently ignored where it should be thrown.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn