To demonstrate the new features in C# 6.0 I have chosen small examples which will help you to understand the C# 6.0 new features easily.
List of new features in C# 6.0
- Manipulating string using string interpolation
- Using null conditional operator
- Using Await in catch and finally block
- Adding Exception Filter
- Auto properties initializer
- Using getter only auto properties
- Converting to expression-bodied function member
- Using Static
Let’s start looking at each feature before C# 6.0 and after this version. Here I am going to explain it one by one with examples.
Manipulating string using string interpolation
It can be looked at as an improvement to the String.Format functionality where, instead of the place holders, you can directly mention the string expressions or variables.
- Place strings where we need them
- Specify a space after/before the string
- Use conditions
- class Program
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string GetFullName(string firstName, string lastName) => firstName + lastName;
- static async void Main(string[] args)
- {
- string firstName = "Jignesh";
- string lastName = "Kumar";
- Console.WriteLine("Full Name of Employee {0} {1}", firstName, lastName);
- // After C# 6.0 Place holder not reqired. You can write like as below
- Console.WriteLine($"Full Name of Employee {firstName} {lastName}");
- Console.ReadLine();
- }
- }
Most of the time developers get this exception: “object reference not set to an instance of an object”. In C# 6 we can handle this easily using NULL Conditional Operator. So we can handle System.NullReferenceException using this operator. Please have a look at the below code andyou will easily understand how it will save time.
- class Program
- {
- static async void Main(string[] args)
- {
- #region Simple Condition
- var employees = new List<EmployeeDetail>();
- // old way of doing first check employee object is not null
- var address = employees != null ? employees[0].address : null;
- // After C# 6.0 check null using ? and assign defaul value if its null
- var addressNew = employees[0]?.address ?? null;
- #endregion
- #region Nested Condition
- //If employee is not null and address not null then return city of adress
- // Before C# 6.0
- if (employees != null && employees[0] != null)
- {
- var city = employees[0].address.city;
- }
- var empCity = employees != null ? (employees[0].address != null ? employees[0].address.city : null) : null; // old way of doing nested condition
- // After C# 6.0
- var employeeCity = employees[0]?.address?.city; //new way to check nested condtions
- #endregion
- }
- }
Prior to C# 6.0, it was not possible to call async operations in catch {} and finally {} blocks by specifying the await keyword due to implementation specific issues in the C# compiler. Now you will be able to call the await statements in catch {} and finally {} blocks without writing any complicated code. The compiler will automatically handle it.
Here is a code snippet to show you how easily you can call the await for async operations in catch {} and finally {} blocks. Most of the time, we need this to log exceptions and/or do some final clean-ups of the resources
- class Program
- {
- static async void Main(string[] args)
- {
- Exception exception = null;
- try
- {
- throw new Exception("Something went wrong");
- }
- catch (Exception ex)
- {
- exception = ex;
- }
- if (exception != null)
- {
- await LogDataAsync("Oops error occuered => ", exception);
- }
- // After C# 6.0 Loging Exception
- try
- {
- throw new Exception("Something went wrong");
- }
- catch (Exception ex)
- {
- await LogDataAsync("Oops error occuered => ", ex);
- }
- finally
- {
- await LogDataAsync("Exception Logging Completed.", null);
- }
- }
- public static async Task LogDataAsync(string message, Exception exception)
- {
- using (var file = File.AppendText("issue.log"))
- {
- await file.WriteLineAsync($"{message} {exception?.ToString()}");
- }
- }
- }

Viknaraj ManogararajahPosted Jul 19, 2018, 6:49 AM
Nice Article...........
Selva MPosted Jul 10, 2018, 11:19 AM
Nice one :)
Jignesh KumarPosted Jun 22, 2018, 3:49 PM
Thanks peter Campbell..
Peter CampbellPosted Jun 22, 2018, 8:03 AM
Great introduction to the features C# 6.0 added. I have personally used the null conditional operator a lot and has simplified my code by removing all those verbose checks for null objects and null properties.
Dipak ZalaPosted Jun 22, 2018, 1:44 AM
Nice Article