Sep
3
2015

CSharp 6 in ASP.NET 5

Many of the new features of C# 6 aim to reduce the amount of code needed for common tasks.  ASP.NET 5 provides a lean, simplified approach to server-side coding for web sites and APIs. So working with ASP.NET 5 and C# 6 should prove the statement “less is more” to be true!

To showcase the new language features of C# 6, I want to do a simple comparison of how ‘it use to be done’ with ‘how it can be done now’.  To start, consider the following class that could be found in the Models folder in an ASP.NET MVC project:

With the new features of C# 6, you can accomplish the same with much less code, as seen here:

The above class is nearly half the lines of the previous definition, yet accomplishes the same exact objective.  Let’s look at how this was accomplished.

Both classes declared three properties, FirstName, LastName, and FullName.  The first and last name properties are read/write and have a business rule of being initialized to a default value, while the remaining property is read-only.  In the ‘before’ approach, this is how the properties were defined:

Now this can be greatly simplified with new features such as auto property initializers and read-only properties with expression bodies as seen here:

After the FullName you will notice the => syntax which indicates the property will contain an expression body.  The expression in this example is a string concatenation of the first name and last name, which is also simplified with the use of string interpolation.  The $”{s1} {s2}” syntax is much easier to read and manage than the String.Format method approach.

Methods can also contain expression bodies, as seen in the GetLastNameFirst method defined here:

Another feature that can help reduce code a little is the use of using static syntax. Consider the final method of the class:

In the code above, instead of typing String.Concat, I could just type Concat. This is possible because of the using static System.String statement.  This allows calls on all static members of the class without typing the class name first.  Depending on how this gets used, this will either simplify or complicate how your code reads, so use with discretion!

The following code is a controller that creates an instance of both classes in order to test the data values:

Compare line 8 with line 12. Notice the nameof keyword? It provides the simple service of returning the name of the variable it references as a string. Very handy for logging and testing!

If you would like to experiment with the language features, check out the interactive C# 6 demo which contains code very similar to what was shown in this post.  If you would like to examine the code in ASP.NET 5, you can view or download it at https://github.com/palermo4/vs2015/tree/master/AspNet5beta7/Starter7

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
blog comments powered by Disqus

Resources

Archives