📜 ⬆️ ⬇️

Future c #

At the NDC 2013 conference, which was recently held in London, Mads Torgersen, one of the developers of the C # language specification, spoke in his talk “The Future of C #” about possible innovations in it.

Primary Constructor

Intended for abbreviated constructor entry with parameters and assigning them to private variables. To use the primary constructor, you must specify the parameters to be passed in the place of the class declaration after its name, just as we specify them in the regular constructor. This automatically creates private variables with the names corresponding to the names of the parameters. A similar construction is in the Scala language.

//  public class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } } //  public class Point(int x, int y) { } 

Auto read only properties

If now use properties with private setter
 public int X { get; private set;} 

or readonly fields
 private readonly int x; public int X { get { return x; } } 

you can just write
 public int X { get; } = 42; 

Using static classes in using directive

When using the using directive, now it will be possible to specify not only namespaces, but also static classes, in order to ensure the possibility of using class methods without specifying its name.
 using System.Math; ... public double A { get { return Sqrt(Round(5.142)); } } 

Property expressions

Declaration of properties in the style of lambda expressions.
 public double Distance => Math.Sqrt((X * X) + (Y * Y)); 

Method Expressions

Same as above, but only for methods.
 public Point Move(int dx, int dy) => new Point(X + dx, Y + dy); 

Using params with IEnumerable

The ability to transfer to methods with a variable number of parameters classes inherited from IEnumerable. If earlier for this purpose it was necessary to convert a class into an array
 Do(someEnum.ToArray()); ... public void Do(params int[] values) { ... } 

now you can just write
 Do(someEnum); public void Do(params IEnumerable<Point> points) { ... } 

However, it is not clear why this is necessary, given that IEnumerable can be passed to methods without the params keyword.

Safe Navigation Operator

Operator? Eliminates the need to check an object for null before calling its properties or methods. Absolutely identical to the similar operator from Groovy .
')
Example:
 var bestValue = points?.FirstOrDefault()?.X ?? -1; 

As you can see, this operator significantly reduces the code, which now would look like this:
 if (points != null) { var next = points.FirstOrDefault(); if (next != null && next.X != null) return next.X; } return -1; 

Type inference for constructor generic class.

C # is able to infer types when calling generic methods. For example, instead of var x = MyClass.Create <int> (24) we can write var x = MyClass.Create (24). But this technique does not work for generic classes. The workaround in this case was the use of a generic method that performs the role of a class factory.
 var x = MyClass.Create(1, "X"); ... public MyClass<T1, T2> Create<T1, T2>(T1 a, T2 b) { return new MyClass<T1, T2>(a, b); } 

In the new version, this restriction will be removed, and we will be able to write code similarly to the methods.
 var x = new MyClass(1, "X"); 

Declaring out parameters in a callee

Perhaps the code will tell about this change best of all.

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


Sources:
damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated
ndclondon.oktaset.com/t-11783
adamralph.com/2013/12/06/ndc-diary-day-3/#more
wesnerm.blogs.com/net_undocumented/2013/12/mads-on-c-60.html

Source: https://habr.com/ru/post/206356/


All Articles