C# 6.0 Features

Recently I have gone through the features which got introduced as part of C# 6.0. Although these features may be known to many C# developers as this release happened in end of year 2014, this article might help some of them who aren’t aware of these features. The main theme for the release of C# 6.0 is developer productivity. With the help of new features introduced with this release, constructs can be written in more precise way with fewer lines of code which enhances more readability.

Let us look into some features in this article.

  • Expression Bodied Members
  • Static using’s
  • Null Conditional Operator
  • String Interpolation
  • Property Initializers

Expression Bodied Members

This feature allows properties, methods and operators to have lambda like expressions instead of statement blocks. Instead of writing the whole property body using getters/setters, we can now just use the lambda arrow (“=>”) to return values.

Let us see few examples to understand the concept.

  1. /*Code Snippet*/
  2. //old way
  3. public string FirstName {
  4. get {
  5. return "Padmalatha";
  6. }
  7. }
  8. //new way
  9. public string FirstName => Padmalatha;

Internally Lambda (=>) will do the work of return statement. This feature reduces the number of lines of code.

Using Static Directive

This directive helps in invoking all the static types without specifying a type name each time explicitly.

When we use static in using statement, it just points to the class mentioned instead of pointing to entire namespace. All the static methods of that class can now be accessed like global methods.

/*Code Snippet*/

Let us look into an example to understand the concept.

  1. //Before C#6.0
  2. using System.Math;
  3. namespace Demo {
  4. public class TourOfCsharp6 {
  5. public class Point {
  6. public double X {
  7. get;
  8. set;
  9. }
  10. public double Y {
  11. get;
  12. set;
  13. }
  14. public double Distance => Math.Sqrt(X * X + Y * Y);
  15. }
  16. }
  17. }
  18. //After C#6.0
  19. using static System.Math;
  20. namespace Demo {
  21. public class TourOfCsharp6 {
  22. public class Point {
  23. public double X {
  24. get;
  25. set;
  26. }
  27. public double Y {
  28. get;
  29. set;
  30. }
  31. public double Distance => Sqrt(X * X + Y * Y);
  32. }
  33. }
  34. }

In the above example, we can access all static methods of Math class in ‘TourOfCsharp6’ Class. In this example we used ‘Sqrt’ static method which is in ‘Math’ class.

By eliminating the need to explicitly reference the Math class each time a member is referenced, the ‘using static’ directive produces much cleaner code.

One more example to demonstrate this is

By using using static System.Console; we can directly access WriteLine() method in the class without referring the type name each time.

Null Conditional Operator

Every .net developer is aware of NullReferenceException, an exception or bug which will be raised when invoking an object which is null before performing sufficient null checking on that object. This operator makes null check much easier making developers job easy.

Let us look into the syntax with an example.

  1. /*Code Snippet*/
  2. //Before C#6.0
  3. public static void OldNullCheck() {
  4. int iLen;
  5. string sFirstName = null;
  6. if (sFirstName != null) {
  7. iLen = sFirstName.Length;
  8. } else {
  9. iLen = 0;
  10. }
  11. }
  12. //After C#6.0
  13. public static void NewNullCheck() {
  14. string sFirstName = null;
  15. int iLen = sFirstName ? .Length ? ? 0;
  16. }

This is one of the amazing feature which makes the code more readable.

String interpolation

We generally concatenate strings using ‘+’ sign or string.Format() method. But now we can use string interpolation which is more readable and easy.

We implement string interpolation by including ‘$’ sign in the beginning of the string, which allows to specify the strings directly in curly braces to show the values in proper formatted manner.

Let us look into all the three ways with an example, so that we can understand this better.

  1. /*Code Snippet*/
  2. //old ways
  3. Console.WriteLine("First Name is " + sFirstName + " and Last Name is " + sLastName);
  4. Console.WriteLine(string.Format("First Name is {0} and Last Name is {1}", sFirstName, sLastName));
  5. //new way
  6. Console.WriteLine($ "First Name is {sFirstName} and Last Name is {sLastName}");

If we can observer above three syntaxes, the third one is more readable. When working with more number of parameters, string formatting with place holders is difficult as the order of the values should be given correctly.

Property Initializers

Before this feature came into existence, we used to initialize the property in the constructor of the class. Now we can initialize the property when defining the property itself as shown below.

/*Code Snippet*/

Consider we have the following class with one property ‘FirstName’ as shown below,

  1. class MyClass {
  2. public string FirstName {
  3. get;
  4. set;
  5. }
  6. }
  7. We used to initialize the property in the constructor as shown.
  8. class MyClass {
  9. public string FirstName {
  10. get;
  11. set;
  12. }
  13. //Constructor
  14. public MyClass() {
  15. FirstName = “Padmalatha”;
  16. }
  17. }
  18. Now we can initialize it directly when defining the property.
  19. class MyClass {
  20. public string FirstName {
  21. get;
  22. set;
  23. } = “Padmalatha”;
  24. }