Earlier this year, Microsoft released C#6.0 and that bought a long a few really nice features that help streamline code. Some features are sadly only make sense with newer versions of the .Net Framework and my day job is stuck on .Net 4.0, but others are there for whatever framework you need.
I haven’t been able to jump on C#6 right away ue to build servers needing to be updated at work, and I suppose i have to admit i could have at home, but didn’t. Since I have done a few months back, there one some features I have I started using as soon as I have been able to and think they really help clean up boilerplate or generally make code better.
1. Auto-Property Initializers
public class MyObject { public MyObject(string name) { Name=name; } public string Name {get;} public DateTime TodaysDate {get; } = DateTime.Now; }
There are two ways of initializing these properties but they both amount to the same thing – at the point of instantiation. They can be assigned a value inline or in the constructor, but nowhere else.
2. Expression body methods and properties
public class MyViewModel { //constructor and stuff ... //property public bool IsEnabled => _MyModel.IsEnabled; //property public string SelectedName => _MyModel.Names.FirstOrDefault(x => x.IsSelected); //method private void OnSelectionChanged() => SelectionChanged?.Invoke(this, new EventArgs()); public event EventHandler SelectionChanged; }
So this is really good for quick and simple methods and properties and removes a lot of boiler plate. Note that the properties are ‘get’ only.
3. Null-Conditional operator (Elvis operator)
This was previewed above, but this ha been really useful in some places, a particular example is the null check around event invoking. One place I thought I’d get more use out of it is in if statements, but actually it’s more awkward there as it makes the statement return bool? (i.e nullable bool)rather than bool. One way around that is to use a the ?? (null-coalescing) operator to give null a value.
public class MyViewModel { //some stuff ... public event EventHandler SelectionChanged; private void OnSelectionChanged() => SelectionChanged?.Invoke(this, new EventArg()); //if there is a selection, return it's length, else -1. public int GetSelectedNameLength() { return _MyModel.Names.FirstOrDefault(x => x.IsSelected)?.Length ?? -1 } public int ChanceItsTheSamePerson(Person a) { if (a?.SelectedName == GetSelectedName ?? false) { return 100; } else { //inspect some other properties and return a value } } }
There are far more of course, and I would like to have made more use of string interpolation which puts variable names directly in a string, replacing string.Format. The downside is that the projects I work on are culture sensitive and require a new .Net 4.6 keyword to take advantage of culture when doing this. It does give me a chance to show static class import though.
using static System.FormattableString; public PrintFruit() { //This bit works for all .Net versions var fruit = "apple"; System.WriteLine($"My favorite fruit is {fruit }"); //By default the culture is current culture. If we want to use Invariant //culture (or any other culture setting) we would need the .Net 4.6 //FormattableString static class, imported above, and it's Invariant method. var appleCost=2.99; var coststring = Invariant($"IN America, they cost ${applecost}"); System.WriteLine(cost); }
So there we are, a brief walk through some of the C# 6 feature I have found useful so far. To find out more about C#6 and it’s new features:
Read: C# : The New and Improved C# 6.0 at MSDN Magazine
Watch: What’s new in C# 6.0 by Mads Torgersen on Channel9