Saturday, January 10, 2015

C# 6.0: The great changes

It's now more than 10 years I'm writing code in C# and I enjoy every part of it. It gets better with every version, and there is no exception in the coming version: C# 6.0.
Everybody who had ever dealt with MVVM pattern, will be well familiar with the INotifyPropertyChange interface. The tedious part of the implementation were the constants (I used to have constants always) you had to define per each publicly exposed property - to store the name of the property.
I recently had a post about how to retrieve the property names dynamically using reflection (here), but still it's not everyone would consider a good option to go with. C# 6.0 mitigates this pain with a new keyword called "nameof".
To get the name of a property you'd simply write: nameof(PropName), and this would return "PropName". Really cool !

And now the other thing I am happy about. Remember the String.Format("Name: {0}", record.Name)  way of doing things ? There were many issues I've seen where updating the format one forgets to add additional parameter, or adds one, but forgets to add the '{X}' placeholder for it - which would cause a runtime exception. C# 6.0 changes the mechanism completely - as now instead of the placeholders you'll just refer to the values within the format string. That's much more readable and safe:
String.Format("Name: \{record.Name}")

And there is much more...
Post a Comment