C# 6.0 is an another version of C# programming language released in the year 2015. C# 6 provides numerous new features which helps developer to write C# code in more clean and optimized way.
Here are top 8 features of C# 6.0 language
Roslyn, a new compiler introduced in C# 6.0. The new version of C# i.e 6.0 contains many new features with may increase the productivity of developers. Let’s explore the list of new features in C# 6.0 which are really significant in use for a programmer.
- Auto Property Initializers
- using static
- Null Conditional Operator
- Exception Filters
- await in Catch and Finally block
- index initializers
- Expression Bodied functions
- String Interpolation
1. Auto Property Initializers
This new feature of C# 6.0 allows you to set an initial value for auto initializer. In earlier C# version this feature was not available.
public class Customer1 { public Guid customerID1 { get; set; } = Guid.NewGuid(); } |
2. using static
This is one of the interesting features of C# 6.0 language.
It allows you to import the static methods of a single class.
for eg –
instead of writing using System;
We can now write.
using static System.Console;
using static System.Math
See below code snippet.
using static System.Console; //Before C# 6.0 Console.WriteLine(“Hello”); //After C# 6.0 WriteLine(“Welcome to C#”); ReadLine(); |
3. Null-Conditional Operator
NULL Reference error always threatens to the developer. More chances are there to show a yellow page with showing this – “NULLReferenceException”. But in C# 6.0 new version, developer’s life is easy and it doesn’t throw NULLReferenceException error. Null-Conditional operator makes null check much easier in C# 6.0. Please check below code snippet.
Product objProduct = new Product(); if (string.IsNullOrEmpty(objProduct.ProductName)) { objProduct = null; } // Before C# 6.0 WriteLine(objProduct != null ? objProduct.ProductName : “Product1”); // from C# 6.0 onwards WriteLine(objProduct?.ProductName ?? “Product2”); public class Product { public string ProductName { get; set; } = string.Empty; } |
4. Exception Filter
I personally love this new feature of C# 6.0 language. Exception filter allows you to show/log different exception messages based on your choice or requirement.
For eg – You want to show different exception messages to your internal and external users. How you will achieve this. Before C# 6.0 it was not possible, but now in C# 6.0, this feature allows you to do the same.
Please see below code snippet.
string user1 = “user”; string user2 = “admin”; string loggedinUser = string.Empty; try { if (loggedinUser == user1) { //Some action //Exception occurred } else if (loggedinUser == user2) { //Some action //Exception occurred } } catch(Exception ex) when(loggedinUser == user2) // This exception for admin { WriteLine(“Hi, There is some issue. Please contact super admin.”); } catch(Exception ex)// This exception for other users { WriteLine(“Hi, There is some issue. Please contact administrator”); } |
5. await in catch and finally block
Earlier versions of C# had multiple limitations while using await expression. Now, C# 6.0 new feature provides asynchronous programming in catch and finally
This is the most awaited feature. Before C# 6.0 it was not possible to use async and await within catch and finally block. But now it is possible in C# 6.0. Please look at below code snippet.
//Asynchronous programming in catch finally public async void CalCDevision(int val1, int val2) { try { int res = val1 / val2; WriteLine(res); } catch (Exception) { await CatchMethod(); } finally { await FinallyMethod(); } } private async Task CatchMethod() { WriteLine(“Exception occured”); } private async Task FinallyMethod() { WriteLine(“Finally Executed”); } |
6. Index Initializer
This C# 6.0 feature makes collection initializers more consistent.
Before C# 6.0 Dictionary Initialization syntax is {“Key”, “Value”}
But now in C# 6.0 we can write like this – [“Key”] = “Value”;
See below code snippet.
//Dictionary initialization Before C# 6.0 var WebPageError = new Dictionary<int, string> { { 404, “Page Not Found” }, { 301, “Moved Permanently” },{ 404, “Internal Server Error” }}; //After C# 6.0 var webPageError = new Dictionary<int, string> { [404] = “Page Not Found”, [301] = “Moved Permanently”, [500] = “Internal Server Error” }; |
7. Expression-Bodied Function
This C# 6.0 new feature allows us to write a function as an expression. This new feature of C# works for methods and properties which are read-only. See below code snippet and you will notice the differences.
//Before C# 6.0 public static int AddNumbers(int x, int y) { return x + y; } //C# 6.0 onwards public static int Add2Numbers(int x, int y) => x + y; |
8. String Interpolation
With this new feature of C# 6.0 way to write Format string has been completely changed. See below code snippet.
string message = “Hi {0}, Welcome to {1}.”; string userName = “User”; string sitename = “SharePointCafe.net”; //Before C# 6.0 string output = string.Format(message, userName, sitename); WriteLine(output); //C# 6.0 message = $”Hi {userName}, Welcome to {sitename}.”; WriteLine(message); ReadLine(); |
How to implement new features of C# 6.0?
If you want to learn the implementation of these C# 6.0 new features, then watch videos on YouTube.
You may watch below videos – C# 6.0 new Features Part1 and Part2
Watch Part 2 of “C# 6.0 new features”
You may read some popular blogs on SharePointCafe.Net |