What’s new in C# 6 Part One

C# 6 introduces a number of new syntax features, as well as a new compiler technology. In this series of articles, I will be exploring some of these features. The main features we will concentrate on in part one of this series are the language (C#) specific ones:

  1. Using Static
  2. Auto-Property Initializers
  3. Strings Interpolation
  4. Nameof Expressions
  5. Null-Conditional operators
  6. Index Initializers
  7. Exception filters
  8. using await in catch and finaly clause
  9. Binary numeric literal and the numeric separator

Using Static:

Consider the following class:

public static class PrintManager {
 public static void Print(string message) 
{ Console.WriteLine(message); }

In C# 6, the Print method can now be called without having to use the full class name. All that is required is to add a using statement (see line 1 in the below snippet):

using CsharpSix.PrintManager;
 namespace CsharpSx 
 class Program {
 public static void staticUsing() {
 var message = "Hello from C# 6"; 

Auto-Property Initializers

Prior to C# 6 Auto-Property Initializers were not possible. C# 6 makes this easy:

     public class User
        public string FirstName { get; set; } = "Sammy";
        public string LastName { get; set; } = "Ageil";
        public List Tasks { get; set; } = new List();
        public bool IsAdmin { get; set; }

All properties (including the list in the above snippet) are set to a default value (think of how easy it will be to create immutable objects in the future).

Strings Interpolation

String Interpolation or Variable Interpolation (also known as variable substitution or expansion) is the process of evaluating a string literal containing one or more placeholders and yielding a result in which the placeholders are replaced with their corresponding values.

In previous versions of C#, the cleanest way to concat or format strings was by using string.format method. Strings interpolation in C# 6 provides us with a much cleaner way and allows us to evaluate expressions in the same time.

Consider the following example (using the above user class):

static void Main(string[] args) {
 var user = new User();
 user.FirstName = "John"; 
user.LastName = "Doe"; 
user.IsAdmin = true;
 user.Tasks.AddRange(new List<string>{"Contact Client A","Contact Client B"}); 
var message= "User : \{user.FirstName} \{user.LastName}\n\{user.Tasks.Count>0 && user.IsAdmin==true ? " You have \{user.Tasks.Count} tasks to complete" : "No Tasks"}";

Running the above code snippet will print the user's first name (John), the user's last name (Doe), evaluate the expression based on the tasks count and isAdmin, and then print "You have 2 tasks". . Changing the IsAdmin or removing Tasks from object initialization will print "No Tasks".

Nameof Expression

nameof Expressions eliminate the need for magic strings.

Instead of this type code (string a magic string in the exception):

 public static void ProcessTask(object @object)
 if (@object == null) throw new ArgumentNullException("@object");

You can eliminate magic strings by using a nameof expression:

public static void ProcessTasks(object @object) {
 if (@object == null) throw new ArgumentNullException(nameof(@object)); 

This concludes part one. In the next parts of this series, we will examine Null-Conditional Operators, Index Initializers, exception filters, using await in catch and finally clause and binary numeric literal and the numeric separator.

Comments are closed