Introduction and Background
Previously on my blog, I had talked about the new features of C# 6, I discussed many things about those features as well as I talked about whether those features are “actually” new of just sugar coats. Anyways, no disrespect to the hard work team had put forth in implementing those features and I personally like most of those features. For those of you who are interested in reading that post of mine, please redirect here: Experimenting with C# 6’s new features. I hope you will like the post and consider it to be worth sharing.
In this post, I am going to talk about using those features in ASP.NET 5! I think, this would be my last post about ASP.NET 5, because later I would be talking about ASP.NET Core 1.0 which was introduced as the new name for the technologies from now on. Anyways, until then I am going to use ASP.NET 5 terminology and I will explain how you can use C# 6 features in your ASP.NET 5 applications, to make the processes even better, performance efficient and more readable if you are a team of programmers working together to bring a major project.
So basically, what you are going to learn in this post is:
- ASP.NET 5: Not very basics, but enough to allow beginners to understand.
- C# 6 features: They have already been discussed, so please read the previous post.
- How to improve performance of your web application!
This is another major post of mine, comprising of both ASP.NET and C# topics. Typically, I will use ASP.NET more than I am going to talk about C# itself so that web developers can gain some benefit from this post of mine. So let’s get started.
Using C# 6 features in ASP.NET
Using C# 6 features in ASP.NET
What we have in ASP.NET is just a framework used for building web applications. C# is just the language that we can use to program the applications. However, the smoother, efficient and efficient the programs there would be, the better your web applications would perform. C# 6 is the latest version of C# programming language and would definitely use Roslyn compiler. You can use this language in your previous versions of ASP.NET, like ASP.NET 4.5. But for the sake of this post, I am going to use
ASP.NET 5!
ASP.NET 5 itself is very fine tuned… But using the power of C# 6, you can make it even better. In this post, I am going to show you a few methods that you can use C# 6 in. I will start in the similar manner that we had previously (in the previous post) and I will explain the code usage in the terms of ASP.NET web application development scenarios, instead of simple C# programming.
String interpolation
Personally, I am a huge fan of this feature because I have waited for this feature for a very long time. I have always been using string.Format function or StringBuilder objects to generate the strings, but using this feature I can easily write the messages that I want to write.
Now, when you are going to write the strings using dynamic data from the users. I would recommend that you write them using the string interpolation. Like this,
- var message = $"Hello {Username}, you have {count} unread messages.";
- // Then you can use this value in views, or back-end model management, or in HTML
- <p>@message</p>
This way, you won’t have to generate the string representations using concatenations, or to create the string builders. I have already demonstrated that this method is similar to what we had as String.Format() function! Only that this method is much better. A real world example of this usage is, that ASP.NET provides interfaces that you can use to trigger SMS and Email notifications. While previously you had to write the following code:
- var message = string.Format("Hello, {0}! Use {1} as your code to activate the service.", username, token);
- // Send the code through SMS or email services
Basically, that is a good approach, mostly developers use concatenation. Which is a really very bad approach to build strings. Instead now you can do the following:
- var message = $"Hello {username}! Use {token} as your code to activate the service";
This is a really very short way of building the strings, and guess what? They always translate down to string.Format function calls. :-)
Conditional try…catch
Now, I have been mostly watching the source codes of programmers to be like:
- try
- {
- // Code that may raise exception.
- } catch (Exception er)
- {
- if (er is NullReferenceException)
- {
- // Log this null error
- } else
- {
- // Chain the exceptions or just leave...
- }
- }
What this is, that it would always enter the catch block. Then, inside that block you would be checking the condition that you want to check. So, in C# 6, you can make it even more readable. You can change that code to be like this:
- try
- {
- // Code that may raise exception.
- } catch (Exception e) when(e is NullReferenceException)
- {
- // Execute this block in case the error was "null" reference.
- } catch (Exception e) when(e is UnauthorizedAccessException)
- {
- // Execute this in case the exception was this one..
- }
- // You can chain the exception conditions...
This way, you can allow the exception to propagate back if you are not interested in logging the error on your end. The condition would be checked against, and then it would continue to next statement if the condition is not met.
name of operator
In ASP.NET environment, I think this variable would have the most use! How? Most of the times, developers have to “guess” the variable and then write its own “hardcoded” name. For example like this,
- string name = null;
- // Now when you will call any function on name,
- // it would raise exception. Like:
- int length = name.Length;
- // Before C# 6, you would do:
- // inside catch block
- var message = "Sorry, name was null.";
That is OK! There is no problem with this one… But, what if you later refactor your code and change the name of that variable? Even if the changes are not going to be reflected on the production environment, you are still going to use the details on your development environment. So what you would have to do is that you would have to change these string literals too, just to depict the changes in the system.
Well, C# 6 got you covered for that. You can now know which variable was the problem in your code.

javierma17 javierma17Posted Jan 27, 2016, 10:59 AM
very nice!!I think you wanted to write: var message = $ "{nameof(Neme)}: {Name}, {nameof(Email)}: {Email}"; and public int Multiply (int a, int b) => a * b;
Arul RPosted Jan 27, 2016, 8:41 AM
Nice
Raja TPosted Jan 27, 2016, 8:06 AM
Nice, Thanks for sharing
Ankit BansalPosted Jan 27, 2016, 6:25 AM
Nice share..thanks..
Mohammed IbrahimPosted Jan 27, 2016, 3:22 AM
nice
Mithun PattankarPosted Jan 27, 2016, 2:48 AM
This is pure C#6 features list, examples are crisp. But how is it related to ASP.NET 5(ASP.NET Core 1.0). These C#6 features are part of entire .NET framework technologies like WPF, Console, Windows Form, ASP.NET MVC 5
sreenivasa kPosted Jan 26, 2016, 2:58 PM
good
Mahesh ChandPosted Jan 26, 2016, 9:57 AM
You should start calling ASP.NET 5 now ASP.NET Core 1 :).