Static Using Syntax
In previous versions of C#, we would need to add the proper using statement, such as System, then we could write the following line of code:
- Console.WriteLine("Hello Abrar!");
With C# 6, you can now add the using statement and reference the WriteLine method by itself as in the following:
Auto-Property Initializers
In the past, we may have created our properties with a get and set and then initialized our constructor with the value that we wanted as shown below.
- using System.Console;
- namespace Console1
- {
- class Program
- {
- static void Main(string[] args
- {
- //Sample One
- WriteLine("Hello Abrar!");
- }
- }
- }
In the past, we may have created our properties with a get and set and then initialized our constructor with the value that we wanted as shown below.
- public class Customer
- {
- public Customer()
- {
- customerID = Guid.NewGuid();
- }
- public Guid customerID
- {
- get;
- set;
- }
- }
We can now reduce this code block to one line as shown below. No longer do we need to create a setter or constructor.
- public class Customer
- {
- public Guid customerID { get; set; } = Guid.NewGuid();
- }
Dictionary Initializers
Many C# developers felt the format shown below was confusing for creating dictionaries, mainly because of the use of brackets and quotation marks for the data.
The compiler team decided to change this and make it more readable with the following format. This new format will only save you a few keystrokes, but it is much easier to read in my opinion.
Exception Filters and Async in a Catch and Finally Block
Exception Filters
Exception filters have been supported in Visual Basic, but are new to the C# compiler. They allow you to specify a condition for a catch block. As shown in the following sample, the last catch statement will fire.
Async in a Catch and Finally Block
I think many developers will love this feature because they often need to log exceptions to a file or database without blocking the current thread. Here is an example of how one would work:
Name of Expressions
How many times in your code do you retrieve the name of a member of your class? You would typically wrap the field in quotation marks as shown below.
The main problem with this is that it forces you to put strings in your code to represent the name of a member. In C# 6.0, there is a new operator called nameof that will allow us to refactor our code to remove these string literals. The sample shown below will show how we could refactor that same method.
This results in cleaner code and safety when retrieving member names.
String Interpolation
Prior to C# 6.0, we typically concatenate two or more strings together in one of the following ways:
In C# 6.0, we have a cleaner format as shown in the first WriteLine call, but we can also put expressions directly in the string literal to evaluate an expression as shown below:
Many C# developers felt the format shown below was confusing for creating dictionaries, mainly because of the use of brackets and quotation marks for the data.
- Dictionary<string, string> customerNames = new Dictionary<string, string>()
- {
- { "Abrar Ahmad", ".net" },
- { "Yogesh", "Java" },
- { "Ganesh", "SQL" }
- };
- public Dictionary<string, string> customerNames = new Dictionary<string, string>()
- {
- ["Abrar Ahmad "] = ". net ",
- ["Yogesh"] = "Java",
- ["Ganesh "] = " SQL "
- };
Exception Filters
Exception filters have been supported in Visual Basic, but are new to the C# compiler. They allow you to specify a condition for a catch block. As shown in the following sample, the last catch statement will fire.
- public static async void DownloadAsync()
- {
- try
- {
- throw new Exception("Error");
- } catch
- {
- await Task.Delay(2000);
- WriteLine("Waiting 2 seconds");
- }
- finally
- {
- await Task.Delay(2000);
- WriteLine("Waiting 2 seconds");
- }
- }
I think many developers will love this feature because they often need to log exceptions to a file or database without blocking the current thread. Here is an example of how one would work:
- public static async void DownloadAsync()
- {
- try
- {
- throw new Exception("Error");
- }
- catch
- {
- await Task.Delay(2000);
- WriteLine("Waiting 2 seconds");
- }
- finally
- {
- await Task.Delay(2000);
- WriteLine("Waiting 2 seconds");
- }
- }
How many times in your code do you retrieve the name of a member of your class? You would typically wrap the field in quotation marks as shown below.
- public static void DoSomething(string name)
- {
- if (name != null) throw new Exception("name");
- }
- public static void DoSomething(string name)
- {
- if (name != null) throw new Exception(nameof(name));
- }
String Interpolation
Prior to C# 6.0, we typically concatenate two or more strings together in one of the following ways:
- string firstName = "Abrar";
- string lastName = "Ahmad";
- WriteLine("Name : " + firstName + " " + lastName);
- WriteLine(string.Format("Name : {0} {1}", firstName, lastName));
- string firstName = "Abrar";
- string lastName = "Ahmad";
- WriteLine("Name : \{firstName} \{lastName}");
src: By Abrar Ahmad Ansari on Apr 10, 2015
http://www.c-sharpcorner.com/UploadFile/d06872/some-new-features-in-C-Sharp-6-0/