New C# 6.0 features
November 14, 2014 by Anuraj
.Net ASP.Net CodeProject Miscellaneous Visual Studio
As part of the Connect(); event, Microsoft introduced VS2015 Preview, which includes C# 6.0 with few more features. (These features are not included in my earlier post What is new in C# 6.0, as I already mentioned these features introduced by Microsoft in the Connect(); event few days back.)
- nameof operator - nameof operator allows developers to use program elements as text. In MVVM project, you are using property notifications, it is recommended to use strongly typed property notifications, instead of string. Because you are hard coding the property name, if you rename the property, you need to change the string manually. The nameof operator helps you to use the property as the parameter.
Runtime will replace the property with the property name while generating the IL code.
- null conditional dot operator - as the name indicates it for null checking. This helps you to make null checking fade into the background.
I have a function like this, which will print the name and price of the book. Please note I am not doing any null check, if the book instance is null, it will throw exception. The null conditional dot operator helps to avoid this validation and processing. If you are invoking a property of null object instance, it will return null for that property as well, it won’t throw null reference exception.
In case of value types, Visual Studio will treat the type of the variable as nullable, if you use null conditional dot operator.
- string interpolation - Another cool feature, which will help you to manage string formatting easy. In the current framework, you need use string.Format function, it little complex, you need to put numeric place holders ({n}) and based on the number you need to set the variables. String Interpolation will help developers to use actual variables as placeholders.
Can be re-write like this.
It supports IntelliSense for the variable names, also variables can be identified with different color.
You can download VS 2015 Preview from here
Happy Coding :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub