(The Roslyn project and documentation has been moved to GitHub)
C# 6.0 is the new upcoming version of C#, it is part of open source compiler platform (Roslyn) by Microsoft. The new compiler is written in C# as well, (How is that possible see this and this)
There are many new features which are planned to be released with the new version, One can find the detailed documentation about them here.
Here are few which have changed from their initial planned version.
Static Import
This feature allows a user to access (allowable) static members of a class/type, without a qualifying class name.
Consider the example of
Console.WriteLine("Some test message");
With the static import, we would be able to to call method “WriteLine” directly without using the class name “Console”.
The current version of C# 6.0 supports Static Import but with the explicit keyword “static” in “using” statement.
using static System.Console; namespace TestApplication { class Program { static void Main(string[] args) { WriteLine("My test message"); } } }
The previous planned version for C# 6.0 had static import feature without “static” keyword in using directive.
“static import” feature might case a breaking change in very rare situation. Consider the following example with respect to “Extension Methods”
Consider the following code example in prior version of C#.
List<int> myList = new List<int> { 1, 2, 3, 4, 5, 6, 7 }; var query = Enumerable.Where(myList, r => r % 2 == 0);
In the code above extension method “Where” is called like a static method, instead of like an instance method on “Enumerable<T>”
But if we use “static” import feature, one would thought that we should be able to do something like:
using static System.Linq.Enumerable; //.... class definition List<int> myList = new List<int> { 1, 2, 3, 4, 5, 6, 7 }; var query = Where(myList, r => r % 2 == 0); //ERROR!
But it will error out with
The name ‘Where’ does not exist in the current context
Since “Where” is defined as an extension method. But using “Enumerable.Range” like below will not cause any error as “Range” is an ordinary static method and not an extension method. :
var items = Enumerable.Range(0, 10); var items2 = Range(0, 10);
So now changing an ordinary static method to extension method could cause a breaking change, which wasn’t the case with prior versions of C#.
Null-Conditional Operator
Only the name/term has been changed, earlier version had Null Propagation operator. The feature is useful in a way that it provides a safe navigation operator on an object properties in case of the object being null.
int[] myArray = null; //...some code int? length = myArray?.Length;