Visual Studio 2015 will be officially RTM on July 20th, Microsoft announced. With it comes a new version of the .NET framework (actually, two), .NET 5.0, and inside of it, support for a new version of the C# language, C# 6.0. Yeah, the numbers don’t match, keep that in mind!
If you are curious, Visual Studio 2015 will also include .NET 4.6. This will provide a migration path for those of us who are stuck with .NET 4.x code and can’t afford the breaking changes that .NET 5 will introduce. More on this in a future post.
For the moment, I am going to talk about what’s new in C# 6.0, in no particular order. Some of you may be familiar with parts of it, and some might even be expecting a bit more, but unfortunately, some features that were previously announced didn’t make it to the final version.
It is now possible to provide an initial value for auto-implemented properties without resorting to a custom constructor. Here’s how, in two examples:
Did you notice how we can even use simple expressions in the initialization block? Pretty cool!
Similar to the previous example, we can now have getter-only properties, that is, without any kind of setter. We just need to provide an initialization value:
Alternatively, we can define its value in a constructor:
Expression-Bodied Methods and Properties
Not sure of the actual interest of this one, but, hey, it’s here: the ability to supply the body for properties and methods from simple expressions.
nameof expressions avoid the need to hardcode names or use complex reflection or LINQ expression tricks. It’s better to show it in action:
This is about the ability to filter exceptions in a catch block based on more than the exception class itself:
Notice that you can use methods in the filter clause, not just simple comparisons.
This is one of my favorites: adding field, variable and method calls in the middle of a string, and having it interpreted at runtime:
Very similar to String.Format, but easier to use.
Another personal favorite! No need for repeated null checks in deep property accesses or event handlers, the compiler takes care of it for us:
If either myInstance or myProperty are null, null will be returned. It even works for events, as you can see.
Using Static Members
Besides bringing namespace types into context, using can now be used to do the same with static methods! For instance, take Console.WriteLine:
This will come in handy, to avoid type clashes. We just import the methods we’re interested in.
Index Member Syntax
A new notation for creating dictionary instances is introduced:
Async in Catch and Finally Blocks
Using async in a catch or finally block is now valid:
Extension Add Methods in Collection Initializers
Starting with C# 4 (.NET 4), we could use an explicit enumeration syntax for creating collections (actually, any kind of classes that provided an Add method with a compatible parameters). Now extension methods are supported as well:
Here we are creating a List of strings, but we have an extension method that knows how to add integers to collections of strings.
All weighed up, there are some nice additions, others I don’t really see the point, but eventually I may.
You can read everything about it in the Roslyn site at GitHub: https://github.com/dotnet/roslyn and https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14.