C# 7.0 and all the new good stuff
It is again another milestone for the C# team, it is now official and the new version of C# is out. This time we are going to talk about C# 7.0 and all the new good stuff.
Historically, C # 3 allows the use of Linq, C # 4 makes dynamic programming possible, C # 5 allows the use of Tasks and version 6 brings several syntactical modifications. The global movement of C # takes us to a much more functional approach.
1. The separators
It is now possible to use separators directly in numeric variables to gain readability. These are very useful when manipulating large numbers.
var i = 100_000;
It is also possible to use them with hexadecimal variables as well as with binary variables.
var hex = 0xAAA_BCD_123; var bin = 0bx101_011;
2. Variables out: simplified passage
Before C # 7, it was necessary to declare the output variables “out” to send them as the argument of a method.
int result; if (int. TryParse ("123", out result)) { }
It is now possible to gain a line of code by implicitly declaring them as parameters of this method.
if (int. TryParse ("123", out int result)) { }
3. Reference variables
To access the reference variables, use the keyword “ref”. It is now possible to store references in local variables.
int[] array = {1, 15, -39, 0, 7, 14, -12}; ref var v = ref array[4]; v = 100; WriteLine (array[4]);
Thus, the code above will display 100 and not 7. The variable v is of type int and it is not a pointer but it references the same memory as array [4]. It is also possible to return a reference at the output of a method.
4. The pattern matching
Functionality expected since a long time, the pattern matching makes us gain another line of code. It is used with the keyword “is”. The following code declares the variable i of type int, locally, for the purpose of testing the type of the variable o.
if (o is int i) { WriteLine($ "{i}"); }
We can also add additional conditions in the if test.
if (o is int i && i > 7) { }
This variable declaration can be used in a switch with the possibility of adding conditions with the where clause.
switch (object) { case int i: Console.WriteLine(i++); break; case string s when(s.Length() >= 10): Console.WriteLine(s.toUppercase()); break; default: break; }
It is therefore important to respect the order of the “box”.
5. The tuples
Tuples avoid creating a new class or structure. Their writing is now simplified.
Before:
static void Main(string[args]) { var data = new [] { 1, 2, 3 }; var result = Sum(data); WriteLine($ "Sum : {result.Item1}, Average : {result.Item2}"); } public static Tuple<int, double> Sum(IEnumerable<int> nums) { int sum = 0; int count = 0; foreach(var i in nums) { sum += i; count++; } return Tuple.Create(sum, (double) sum / count); }
An interesting novelty is the ability to use tuples types and literals. With Visual Studio 15 preview 4, you still have to reference the ValuesTuples package on Nuget.
Now :
static void Main(string[args]) { var data = new [] { 1, 2, 3 }; var (s, a) = Sum(data); // tuple deconstruction WriteLine($ "Sum : {s}, Average : {a}"); } public static(int Sum, double Average) Sum(IEnumerable<int> nums) { int result = (Sum: 0, Count: 0); foreach(var i in nums) { result.Sum += i; result.Count++; } return (result.Sum, (double) result.Sum / result.Count); }
Note that the Sum method returns the tuple (int Sum, double Average) generated by the compiler. The deconstruction of the tuple was possible with var (s, a) = Sum (data);
It is possible to customize the deconstruction method:
public double Width { get; set; }; public double Height { get; set; }; public void Deconstruct(out double width, out double height) { width = Width; height = Height; }
6. Local functions
With C # 7, we can declare local functions in our methods while using recursion.
public static int Fibonacci(int x) { (int current, int previous) Fib(int i) { if (i == 0) return (1, 0); var (p, pp) = Fib(i - 1); return (p + pp, p); } if (x < 0) throw new ArgumentException("Less negativity please !", nameof(x)); return Fib(x).current; }
By combining local functions with the use of tuples, the writing of the Fibonacci function is greatly simplified.
In conclusion
C# 7 will not revolutionize the way we program but brings new and exciting features.
In recent years, with the opening of Roslyn and the ability to run .NET core on other platforms such as mobile or Linux, Microsoft is committed to open source and interoperability.