Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


 
 

New Features to Enhance Developer Productivity in C# 6.0

Posted by Sandeep Chanda on Jul 8, 2016

A lot of focus on C# version 6.0 has been towards enhancing the productivity of developers. Attention has been paid to areas where the same functionality can be attained by writing fewer lines of code, making it look clean and much easier to maintain. Similarly, the team has also worked towards improving the readability of string manipulations in code in terms of how they are formatted in output, conceiving the idea of interpolated strings. Then there are improvements in the area of exception handling. In this post we will discuss, in particular, the idea of exception filters that was newly introduced in C# 6.0.

First let us explore the idea of expression bodied members. Expression bodied members are a first class citizen in C# 6.0 and reflect a syntax that is a combination of the current syntax for members and the lambda expression syntax. The following code illustrates-

Let's say we had a method that returns the sum of two numbers. A standard syntax could look like:



public int Add(int a, int b)
{
    return a + b;
} 

The similar syntax could be demonstrated using an expression body which is much simpler to represent:

public int Add(int a, int b) => a + b; 

It can be applied to asynchronous operations as well:

public async Task<string> Request() => await Response();

Expression body also makes it simpler to represent properties as illustrated in the following code example:

public DateTime CurrentDateTime => DateTime.Now;

This is the equivalent representation of:

public DateTime CurrentDateTime
{
    get
    {
        return DateTime.Now;
    }
}

Note that there are currently some limitations to leveraging the expression body syntax, especially for branching statements such as if else and switch. Only the tertiary operator works for conditional assignment.

C# 6.0 introduces the idea of interpolated strings that essentially allows developers to use named variables in strings instead of the indexes.

The following code illustrates the comparison:

string name = string.Format("name: {0} {1}", person.FirstName, person.LastName);

In the interpolated format you can use:

string interpolatedName = $ "name: {person.FirstName} {person.LastName}";

This is a much cleaner way to represent formatted strings in code.

Finally, C# 6.0 also introduces the idea of exception filters that allows you to filter exceptions based on a particular condition using the "when" statement. It is not only a syntactical improvement for productivity and cleaner code representation, but also preserves the stack trace, thereby providing better traceability than an if condition to represent the equivalent filter in regular syntax.

TAGS:

productivity, Exception handling, C# 6


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date