There are plenty of blogs and articles already posted about C# 6.0 new features. But I observed there are some changes from the features in the preview and newly shipped with Visual Studio 2015 RTM (of course some improvements are based on feedback from the preview). In this article, I'm trying to address those changes as well as I will also describe new features.
The following is the list of enhancement delivered in the RTM version of C# 6.0 (excluding VB enhancements that were specified in the source, also I have added quick samples to illustrate the enhancements).
Feature |
Example |
Auto-property initializers |
public int X { get; set; } = x; |
Read-only auto-properties |
public int Y { get; } = y; |
Constructor assignment to getter-only autoprops |
public DemoClass{ Y = 15; } |
Static imports |
using static System.Console; … Write(4); |
Index initializer |
> new JObject { ["x"] = 3 } > var numbers = new Dictionary<int, string> { [7] = "seven", [9] = "nine", [13] = "thirteen" }; |
Await in catch/finally |
try … catch { await … } finally { await … } |
Exception filters |
catch(E e) when (e.Count > 5) { … } |
Partial interfaces |
Partial Interface I1 |
Multiline string literals |
"Hello<newline>World" |
Expression-bodied members |
public double Dist => Sqrt(X * X + Y * Y); |
Null-conditional operators |
customer?.Orders?[5] |
String interpolation |
$"{p.Name} is {p.Age} years old." |
nameof operator |
string s = nameof(Console.Write); |
#pragma |
#Disable Warning BC40008 |
Read-write props can implement read-only interface properties |
<"In my TODO items"> |
#Region inside methods |
public void TestMethod() { #region MyRegion
#endregion } |
CRef and parameter name |
/// <summary> /// Initialize with <see cref="Program(DateTime)"/> /// </summary> public Program(DateTime timestamp) {
} |
Extension Add in collection initializers |
See the explained example below. |
Improved overload resolution |
<"In my TODO items"> |
Most of the feature enhancements specified above are self explanatory. So I'll be explaining only the interesting ones that need explanation.
Properties Enhancements
We have a couple of enhancements in the Auto properties. With the new enhancements the compiler generated the required code and saved some developer's time. Now the properties are:
- public class Person
- {
-
- public int Id { get; }
-
- public Person()
- {
-
- this.Id = -1;
- }
-
-
- public string Name { get; set; } = "Jhon Doe";
-
- public int Age { get; set; }
-
- public string Title { get; set; }
- }
Note: Static properties are not allowed to have constructor initialization.
Static Imports and Extension methodsNow C# allows importation of static members of another class without using the class name. So they can be used directly in another class. This feature is extended to include the Extension methods. Also the improvement was done with Using. Now one must use the Static keyword to import the static members.
For example:
- using System.Text;
- using System.Threading.Tasks;
-
- using static System.Console;
- using static System.Linq.Enumerable;
- public class Demo
- {
- public void Print()
- {
- var cultures = new string[] { null, "en-NZ", "en-US", "en-AU", "en-GB" };
- Write("The static methods are directly available.");
-
-
-
-
-
-
- WriteLine(Range(0, 10).Where(x => x % 2 == 0));
- }
- }
Extension Add method for Collection Initializers
This looks interesting but this feature already exists in VB.NET, surely it was brought back home to C#. It could be value added where a Generic collection must be initialized with complex types. Here's a sample of how it works.
For example the following is a simple entity representing a complex type (let's assume).
- public class Person
- {
- public int Id { get; set; }
-
- public string Name { get; set; }
-
- public int Age { get; set; }
-
- public string Title { get; set; }
- }
Now C# 6.0 allows us to write an extension Add method for a Collection, for example:
- public static class Extensions
- {
- public static void Add(this List<Person> source, int id, string name, int age, string title)
- {
- source.Add(new Person {
- Id = id,
- Name = name,
- Title = title,
- Age = age
- });
- }
- }
If we want to use this collection of type Person in the code then the collection initializer would be less work as in the following:
- List<Person> List = new List<Person>()
- {
- { 1, "Ramesh", 22, "Mr." },
- { 2, "Julia", 24, "Ms" },
- { 3, "Paula", 12, "Mrs." },
- };
This might not look like a big change but will surely add some value in some cases, for example unit tests where collections are to be created for fake data.
I hope most of us will be enjoying these features though it will take some time to decide if they are a handy feature. Those that are marked as a “TODO” item, I'll be working on exploring them and may be writing about them in a future article.