C# 6.0 : Something new on it’s way

As we know C# 6.0 is about to come with Visual Studio 2015, Let us see some of the awesomeness which will be gifted to us with C# 6.0

First of all Let us start with the part which a developer always gets, even after taking lots of care, is Exceptions!!

1) Conditional Catch Blocks

As a developer there might be a need that we require some conditions on Try.. catch block to catch some particular type of exception.

For that earlier we needed to do like below :-

Earlier:-

try
 {}
 catch (Exception ex) 
 {
 if (ex.InnerException.Message == "Parameter Error") 
{ 
} 
throw; } 

Now in C# 6.0 developers can now do the same by adding a if test to the catch statement.
Now:-

try
{
 ...code...
 }
 catch (Exception ex) when (ex.InnerException.Message == "Parameter Error")
 {
 ...error handling code...
 }

2) Wait over for “await” in Catch Block

Have you ever wanted “await” in catch block? Your search is over because C# 6.0 would allow you to add await keyword in catch block.

Earlier :-

try
{
 result = await wc.DownloadStringTaskAsync( new Uri( "http://badurl" ) );
 downloadSucceeded = true;
}
catch
{
 downloadSucceeded = false;
}

if (!downloadSucceeded)
 result = await wc.DownloadStringTaskAsync( new Uri( "http://fallbackurl" ) );

Now:-

try 
{ 
 //Do something
}
catch (Exception)
{
 await Logger.Error("exception logging")
}

3) Easy property initialize-rs

As you know till now if we want to initialize properties we always needed a constructor and then we initialize our auto incremented property as below :-

Earlier:-

public int Id { get; set; }
public YourClass()
{
Id = 1;
}

In C# 6.0, you can also initialize that property to some constant value in the same statement, like this:

Now:-

public int Id { get; set; } = 1;

4) Awesome Index Initializers

Suppose you want a Dictionary object for let us say key value pair of int and string so you would write something as below :-

Earlier:-

Dictionary<int, string> students = new Dictionary<int, string>()
 {
 { 1, "Student1" },
 { 2, "Student2" },
 { 3, "Student3" },
 };

C# 6.0 gives you a new syntax that saves a few keystrokes but, more importantly, makes the resulting code easier to read by eliminating some curly braces and leveraging the equals sign. The new syntax looks like this:

Now:-

Dictionary<int, string> newRomanNumerals = new Dictionary<int, string> ()
 { 
 [1] = "Student1",
 [2] = "Student2",
 [3] = "Student3"
 };

5) 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.
Sometimes when we use String.Format(), the numbered placeholders like {0}, {1}, {2}, etc. caused the problem (especially when you use more placeholders) due to arguments to be supply in the same order.

Earlier:-

var statement = string.Format("This is one of the features of {0}", lng.CurrentLanguage);

Now:-

var statement = $"This is one of the features of {lng.CurrentLanguage}";

6) More Power to Static Classes

Till now whenever we reqired to use Static objects we needed name of the class before the object like below :-

Earlier:-

YourClass.SomeMethod();

C# 6.0 allows us to import the Static classes and use the static members directly without class name.

Now:-

using YourClass;
 
 SomeMethod();

7) OUT Parameter Declaration

It was always required for OUT paramater to be declared before the use of that parameter as below :-

Earlier:-

DateTime dateTimeValue;
 DateTime.TryParseExact(parameterValue, "MM-dd-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeValue);

C# 6.0 allows you to declare the OUT parameter during the method call, as shown below.

Now:-

DateTime.TryParseExact(parameterValue, "MM-dd-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTimeValue);

8) Null-conditional operators

What if you want to check for null in C#?

Well suppose i have some varaible named result and i want to check for null then:-

Earlier:-

var requiredResult = result == null ? 0 : result.myResult;

C# 6.0 introduces Null-conditional (?.) operator which works on top of conditional (?:) operator to provide the much easier access to check the NULL values.

Now:-

var requiredResult = result?.myResult;

9)  Roslyn—A New Compiler for C#

Yes, new compiler for C# as well as for VB which is open source and can be found at Github.

So, I hope it would be helpful to you and I will add more as soon as i get to know more and more beauty of C# 6.0!

Till then Happy Coding!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: