January 19, 2015 by Christoff Truter C#
While doing my research for this post, I ran into quite a few sources containing features that doesn't actually seem to exist in C# 6.0. This is thanks to early released previews to the public that contained features that the guys at Microsoft decided to rather exclude or didn't include for preview yet.
The following post contains a list of features added to C# 6.0, based on the VS2015 preview, not sure how close we are to the official release, soooo this might, perhaps, maybe, possibly, conceivably, for all we know be some of the new C# features...
Auto-property initializers
It is now possible to assign values to auto-properties like so.
public string FirstName { get; set; } = "John";
public string FirstName { get; } = "John";
Dictionary<string, string> LOTR = new Dictionary<string, string>() { ["Gandalf"] = "Wizard", ["Legolas"] = "Sindarin Elf", ["Frodo"] = "Constipated Hobbit" };
int? numberOfProducts = products?[1].Colours?.Count();
using System.Console; class Program { static void Main(string[] args) { WriteLine("What sorcery is this?"); } }
try { throw new Exception("Dude"); } catch (Exception ex) if (ex.Message == "Where is my car?") { }
Expression-bodied members
Another not so life changing addition, is that we can now use expressions as our member bodies as an alternative to statement blocks (and the crowd goes mild - yay).
public double MyAge() => (DateTime.Now - new DateTime(1983, 2, 2)).TotalDays / 365;
string s = "\{p.FirstName} \{p.LastName}";
But apparently this will be a bit cleaner in the final release and look something like this:
string s = $"{p.FirstName} {p.LastName}";
Console.WriteLine(nameof(p)); // prints p