Mads Torgersen (Program Manager for Managed Languages) from the C# design team covered some of the new probable features in C# 6.0 in Build 2014 (Click here to view the video).
Lets drill down some of the highlighted features
All these features look pretty interesting. Looking forward to C# 6.0
Happy Coding ...
Lets drill down some of the highlighted features
Auto-Properties w/ Initializer:
Initializing a immutable class is a tedious process currentlyBefore
private int _x;
public int X { get { return _x; } }
public int Y { get; private set; }
With C# 6.0
public int X { get; } = _x;
public int Y { get; } = _y;
Thoughts
Instead of creating private members to set the value and return in getter or use private setter. We can initialize the property like this. Very useful for the immutable classes.Primary Constructor:
Shorter and cleaner way to write constructor where we are just setting the values to the properties with private set without any extra validation.Before
public class Person
{
private string _name;
public Person(string name, int age)
{
_name = name;
Age = age;
}
public string Name { get { return _name; } }
public int Age { get; private set; }
.....
}
With C# 6.0
public class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
.....
}
Thoughts
Combination of auto properties initializer and primary constructor will simplify C# code a lot.Static using statement:
Now in C#6.0, we can include static classes in the scope.Before
using System;
......
public double Distance { get { return Math.Sqrt(16); } }
With C# 6.0
using System.Math;
......
public double Distance { get { return Sqrt(16); } }
Thoughts
Nice to have feature. It can help in reducing lot of key strokes of including static class name again and again.Property/Method Expression:
Property and method syntax simplified for short hand. Looks more like LINQ statement but has nothing to do with it.
Before
public double Distance { get { return Math.Sqrt(16); } }
public Point Move(int dx, int dy)
{
return new Point(x + dx, y + dy);
}
With C# 6.0
// Property expression
public double Distance => Math.Sqrt(16);
// Method expression
public Point Move(int dx, int dy) => new Point(x + dx, y + dy);
Thoughts
Nice to have feature.Declaration Expression:
Instead of limiting variables to declaration statement, going forward we can declare them in the middle of the expression where they are need
Before
int val;
var isValid = int.TryParse ("1231", out val);
With C# 6.0
var isValid = int.TryParse ("1231", out var val);
Thoughts
Nice to have feature.Params for Enumerable:
Now will be able to send params of enumerables instead of limiting to arrays.
Before
public void SomeMethod(params int[] vals)
{
.....
}
With C# 6.0
public void SomeMethod(params IEnumerable<int> vals)
{
.....
}
Thoughts
Again, nice to have feature. Will not have to convert enumerables into array anymore for the params.Monadic null check:
Removes the need to check for nulls before accessing properties or methods. Known as the Safe Navigation Operator in Groovy).
Before
if (points != null) {
var next = points.FirstOrDefault();
if (next != null && next.X != null) return next.X;
}
return -1;
With C# 6.0
var bestValue = points?.FirstOrDefault()?.X ?? -1;
Thoughts
Awesome. Will reduce lot of noise in the code.Await Calls from Within a Catch Block:
With C# 6.0
try
{
WebRequest webRequest =
WebRequest.Create("http://IntelliTect.com");
WebResponse response =
await webRequest.GetResponseAsync();
// ...
}
catch (WebException exception)
{
await WriteErrorToLog(exception);
}
Thoughts
Have not used a lot. No Comments.All these features look pretty interesting. Looking forward to C# 6.0
Happy Coding ...
No comments:
Post a Comment