Code Less in C#6 Posted on September 22, 2015 by bmedina When a new version of any technology comes out they usually boast something that would make that version stand out. In C#6, the main focus was on the small features and improvements in order for C# developers to write better and cleaner code. Let’s find out how many new features are in this sample code: C# using System; using static System.Console; public class Program { public static void Main() { var developer = new Developer() { Name = "Benj", YearsOfExperience = 2 }; developer?.DisplayDeveloperInfo(); } } public class Developer { public Guid Id { get; } = Guid.NewGuid(); public string Name {get; set; } public string CurrentProject {get; set; } = "Some Project"; public int? YearsOfExperience { get; set; } public void DisplayDeveloperInfo() => WriteLine($"Developer ID: {this.Id}nName:{this.Name}nProject:{this.CurrentProject}nYears of Experience:{this.YearsOfExperience}"); } 1234567891011121314151617181920 using System;using static System.Console; public class Program{ public static void Main() { var developer = new Developer() { Name = "Benj", YearsOfExperience = 2 }; developer?.DisplayDeveloperInfo(); }} public class Developer{ public Guid Id { get; } = Guid.NewGuid(); public string Name {get; set; } public string CurrentProject {get; set; } = "Some Project"; public int? YearsOfExperience { get; set; } public void DisplayDeveloperInfo() => WriteLine($"Developer ID: {this.Id}nName:{this.Name}nProject:{this.CurrentProject}nYears of Experience:{this.YearsOfExperience}");} If you’re wondering if that program compiles, yes, it does! It may look strange because of some of the “little changes” in C#6, but it would not be surprising if you start to use these features regularly once you’ve learned about them. Now let’s go deeper in the sample code. In C#6, it is now possible to use the static keyword on imports. What this enables us to do is to directly call on a static member of the namespace without the need to include the prefix (i.e. the class name). WriteLine($"Developer ID: {this.Id}nName:{this.Name}nProject:{this.CurrentProject}nYears of Experience:{this.YearsOfExperience}"); 1 WriteLine($"Developer ID: {this.Id}nName:{this.Name}nProject:{this.CurrentProject}nYears of Experience:{this.YearsOfExperience}"); If you noticed that in that line, normally we write like this: WriteLine("Developer ID: 1nName:BenjnProject:Some ProjectnYears of Experience:2"); 1 WriteLine("Developer ID: 1nName:BenjnProject:Some ProjectnYears of Experience:2"); In C#6, we can omit including the name of the class (i.e Console) and it would still compile. Looking at the content of the WriteLine above the string that it will write looks different as well because another feature that C#6 introduced is string interpolation. What it does is it allows us to format the string without using the string.Format(“{0},{1},{2}”, firstName, lastName, gender) method anymore. This is one feature that would be used a lot because of the impact that it has. By using the string.Format() approach, we are prone to commit mistakes when we assign the values with the correct order and the line of code becomes less readable as well as it expands. With string interpolation, it will boost the readability and it will have a positive effect on the maintainability of the code as well. The last strange thing in the WriteLine syntax in the sample above is the ?. symbol as shown in anotherDeveloper?.Name. That is the null operator (a.k.a. the Elvis operator: because the symbol looks like it has two dots for the eyes and the swirl of the question mark looks like Elvis’ hair). Anyway, the null operator is another great way for us to trim down some lines of code. Let’s look at a bigger piece of code as an example: // Let me update the models that we will use for this example public Developer { public Project CurrentProject { get; set; } // Changed this from: public string CurrentProject {get; set; } = "Some Project"; } public class Project { public Guid Id { get; } = Guid.NewGuid(); public string Name { get; set; } public void DisplayName() => WriteLine($"You are assigned to the {this.Name} project."); } public static void Main() { var developer = new Developer() { Name = "Benj", YearsOfExperience = 2 } developer.CurrentProject = new Project() { Name = "Some Project"}; /* This is how we check for normally check for null values: if(developer!=null) { developer.DisplayInfo(); } */ developer?.DisplayInfo(); // this line does the same thing as the commented snippet above. /* Now, it gets more fun. This is how we check for normally check for nested null values: if(developer!=null && developer.CurrentProject != null) { developer.CurrentProject.DisplayName(); } */ developer?.CurrentProject?.DisplayName(); // this line does the same thing as the snippet above. // Can the null operator have an else clause as well? Of course. Developer anotherDeveloper = new Developer() { Name = "Ralph", YearsOfExperience = null }; /* This is how we check for normally null values with another condition: if(anotherDeveloper!=null) { return anotherDeveloper.YearsOfExperience!=null ? anotherDeveloper.YearsOfExperience : 0; } */ var result = anotherDeveloper?.YearsOfExperience ?? 0; // this is the same as the snippet above WriteLine($"Hello {anotherDeveloper?.Name} you have spent {result} year/s as a developer."); } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 // Let me update the models that we will use for this example public Developer { public Project CurrentProject { get; set; } // Changed this from: public string CurrentProject {get; set; } = "Some Project";} public class Project { public Guid Id { get; } = Guid.NewGuid(); public string Name { get; set; } public void DisplayName() => WriteLine($"You are assigned to the {this.Name} project.");} public static void Main(){ var developer = new Developer() { Name = "Benj", YearsOfExperience = 2 } developer.CurrentProject = new Project() { Name = "Some Project"}; /* This is how we check for normally check for null values: if(developer!=null) { developer.DisplayInfo(); } */ developer?.DisplayInfo(); // this line does the same thing as the commented snippet above. /* Now, it gets more fun. This is how we check for normally check for nested null values: if(developer!=null && developer.CurrentProject != null) { developer.CurrentProject.DisplayName(); } */ developer?.CurrentProject?.DisplayName(); // this line does the same thing as the snippet above. // Can the null operator have an else clause as well? Of course. Developer anotherDeveloper = new Developer() { Name = "Ralph", YearsOfExperience = null };/* This is how we check for normally null values with another condition: if(anotherDeveloper!=null) { return anotherDeveloper.YearsOfExperience!=null ? anotherDeveloper.YearsOfExperience : 0; } */ var result = anotherDeveloper?.YearsOfExperience ?? 0; // this is the same as the snippet above WriteLine($"Hello {anotherDeveloper?.Name} you have spent {result} year/s as a developer.");} The null conditional operator is a great feature as it would trim down a bunch of lines of codes whenever it can be applied. This is one of my favorites in the new features of C#6. Note: Make sure that the data type of the variable that you will use is nullable or else it would result to a compiler error. The next feature is being able to set a default value for an auto-property. Let’s have a look in the example: /* This is how we can set a default value for a property before. private readonly Guid id; public Guid Id { get { return id; }} public Constructor() { id = Guid.NewGuid(); } */ public Guid Id { get; } = Guid.NewGuid(); //// this is the same as the code above 123456789 /* This is how we can set a default value for a property before. private readonly Guid id; public Guid Id { get { return id; }} public Constructor() { id = Guid.NewGuid(); }*/ public Guid Id { get; } = Guid.NewGuid(); //// this is the same as the code above Lastly, we have the expression bodied methods feature that can serve as an alternative way on how we write single line methods. Here’s an example on how I applied it: /* This is how we write it before. public void DisplayProjectName() { WriteLine($"You are assigned to the {this.Name} project."); } public string GetProjectName() { return this.Name; } */ public void DisplayProjectName() => WriteLine($"You are assigned to the {this.Na me} project."); public void GetProjectName() => this.Name; 12345678910111213141516 /* This is how we write it before. public void DisplayProjectName() { WriteLine($"You are assigned to the {this.Name} project."); } public string GetProjectName() { return this.Name; }*/ public void DisplayProjectName() => WriteLine($"You are assigned to the {this.Name} project.");public void GetProjectName() => this.Name; What using static on imports, string interpolation, null conditional operator, setting default values to auto-properties and expression bodied methods all have in common is that it enables us to do the same things with less code. Does having less code automatically means that we would have cleaner codes? That becomes subjective and every developer is entitled to have an opinion whether to write it the new way as shown in this post or to stick in the old way to do it. Just like in the expression bodied method example where even if it looks cleaner, at first sight, but it is more difficult to understand than the old way we do it. To get the most out of these features, we need to be consistent if we choose to go with it. Make it a standard in the project, inform and make sure every developer writes it the same way. Nonetheless, these new features could really become the norm soon. There are still a bunch of cool new features of C#6 like the exception filters. Would you apply these new features in your code base? Which feature did you like the most? Feel free to discuss it below.