top of page

C# 6.0 way changer in writing code.


These days i am hearing so much about the C# 6.0. MS has added couple of syntax shortcuts, reduces the complexity of some scenarios and made code looks clean. In terms of features there is not a drastic change in C# .6.0. In this blog i will be discussing about the new features and their practical uses , so let's get started.

String Interpolation: C# 6.0 introduces JSON like syntax for string interpolation. We used to do string.format and then {0}{1}{2} and keep adding corresponding parameter in the method. But with that, composite formatting wasn’t the most readable thing. But now the syntax is pretty simple.

String para = ${ClassProperty.name}" any string "{AnotherClassProperty.name};

Null Conditional Operator: This features i believe MS copies from java-script, but this is really cool. Generally to access the property of any object we writes object.property but what if the object is itself null then this will throw null exception. To handle this one solution is to check for null one but if we have a long nested property then writing null check will be tedious. To remove that C# 6 introduces null check operator. We just need to add "?" after each object or property. Let's see an example of this:

class Program { static void Main(string[] args) { Program myProgramObject = new Program(); Console.WriteLine(myProgramObject.Name); } public string Name { get; set; } }

This is very simple example but what if because of some reason myProgramObject is null. When control flow will come to myProgramObject.Name it will throw null exception. With C# 6.0 we can just add a check to validate the null. Like myProgramObject?.Name. If the myProgramObject is null then this check will not allow the control flow to execute further. This check is basically writing this code internally

( myProgramObject != null) ? (myProgramObject.Name: null)

Using static: using static directive renders static methods available in global scope, without a type prefix of any kind. For example System.IO.Path is a static class in C# now to use the Combine method we can just call Combine without writing any prefix of that class. We just need to use the namespace of System.IO.Path class as using static directive. For example

using static System.IO.Path;

class Program { static void Main(string[] args) { Console.WriteLine(Combine("firstPath","SecondPath")); } }

But this features as it's own limitation as well for example if there are method having same name in different namespace then the call will be ambiguous, therefore CLR will throw compilation error.

Expression Bodied Methods and Auto-Properties: This feature exists for both properties and methods and allows the use of the arrow operator (=>) to assign an expression to either a property or method in place of a statement body. For example

public class Person { public Person(string name) { Name = name; } public Person(string firstName, string lastName) { Name = $"{firstName} {lastName}"; Age = age; } public string Name { get; set; } public string FirstName => Name.Split(' ')[0]; public string LastName => Name.Split(' ')[1]; public override string ToString() => "\{Name}(\{Age}"; }

Just to mention that C# 6.0 requires C# 6.0 compiler to be there in your machine, if you are working on VS 2015 then compiler comes with that.

That's all i have for this blog, please add your comments about the new features of C# 6.0 , till then bye bye folks..


  • Facebook Social Icon
Recent Posts
Search By Tags
No tags yet.

© 2016. The Tech Assembly.

bottom of page