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:
- using System.Console;
- namespace Console1
- {
- class Program
- {
- static void Main(string[] args
- {
-
- WriteLine("Hello Abrar!");
- }
- }
- }
Auto-Property InitializersIn 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;
- }
- }
Dictionary InitializersMany 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" }
- };
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.
- public Dictionary<string, string> customerNames = new Dictionary<string, string>()
- {
- ["Abrar Ahmad "] = ". net ",
- ["Yogesh"] = "Java",
- ["Ganesh "] = " SQL "
- };
Exception Filters and Async in a Catch and Finally Block
Exception FiltersException 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");
- }
- }
Async in a Catch and Finally BlockI 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");
- }
- }
Name of ExpressionsHow 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");
- }
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.
- public static void DoSomething(string name)
- {
- if (name != null) throw new Exception(nameof(name));
- }
This results in cleaner code and safety when retrieving member names.
String InterpolationPrior 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));
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: