New Features to Enhance Developer Productivity in C# 6.0

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 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.

try    {        ///try body    }    catch (HttpException ex) when (ex.ErrorCode == 500)    {        ///handle exception    } 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

6 Tips for Setting Up a Decentralized Exchange

6 Tips for Setting Up a Decentralized Exchange

There’s no doubt that cryptocurrency is a complex and divisive topic in the modern financial landscape. There are those who are convinced that it’s nothing more than a bubble, but both who are well-informed are able to see the ways in which cryptocurrency can help them both build their fortune

user experience with someone on their phone

5 Ways to Improve Your Customers’ User Experience

They say you can’t judge a book by its cover, but just because they say that doesn’t mean it’s true. Consider how often you choose one sort of product over another just because it appeals to you. Then think about how often you’ve stopped using something because, simply put, it

How to Manage Your Finances after Buying a Home

How to Manage Your Finances after Buying a Home

Buying a home is a milestone in the journey of life – it’s one way to invest your money and create lasting memories. Now you know everything about a home purchase, home mortgage, and what is a conventional 97 loan but do you know what’s next after you sign the

Windows Logging is one of the vital aspects of any Windows system administration. However, it is mostly overlooked until the system develops a problem. This is because logs contain important information needed to troubleshoot and resolve system issues.

The Fundamentals of Windows Logging

Windows Logging is one of the vital aspects of any Windows system administration. However, it is mostly overlooked until the system develops a problem. This is because logs contain important information needed to troubleshoot and resolve system issues. Without it, tech experts might find it difficult to track a computer’s

Interstitial Ads: Best Practices for Successful Campaigns

Interstitial Ads: Best Practices for Successful Campaigns

Interstitial Ads: Best Practices for Successful Campaigns Interstitial ads are full-screen advertisements that appear to grasp the attention of on-site prospects, creating opportunities for brands seeking effective ways to communicate their proposition of value. With such an attention-grabbing format and high-impact visuals, it’s no wonder why interstitial advertising is proving