sábado, 4 de octubre de 2014

What's going on with C# 6.0!

Hello! This is Juan Antonio, back again into the arena.
I'm sorry, it's been a long time since I last wrote a post back in August.
Certainly there have been lots of things to take care off, but I don't want to stop sharing things with my friends.

A couple of days ago an interesting Pluralsight course fell into my hands, What's new in C# 6.0, by Scott K Allen: http://www.pluralsight.com/courses/csharp-6-whats-new
I really recommend if you can watch it not only because of the content but also because Scott is a great speaker. I felt really amazed with what is coming in C# world.
In this post I wanted to summarize the new features of the language.

Everything I'll tell is possible because of the new compiler, .Net Compiler Platform "Roslyn". It is an open source project that we can download and see the source code (http://roslyn.codeplex.com/). It comes along with a new Visual Studio 2014. When I'm writing this, every of these things are in beta testing version so one can download everything for free.

Autoproperty initializers:

With C#6.0 we can set the initial value of properties easily:


See how we can initialize even a read only property.

Primary Constructors:

Primary constructor allows us to define a class constructor and capture the constructor parameters to initialize class properties.

In C# we have a common pattern, we inject a component through a constructor and we use this injected component throughout the rest of the class:



With Primary Constructor we don't need to specify the constructor, instead we can use primary constructor together with autoproperty initializers:


The code becomes more elegant and concise and the benefit is that the menu primary contructor variable is available in the rest of the class as well.

Dictionary initializers:

Initializing a dictionary is now more concise with the new syntax:


Event initializers inside constructors:

Until C#6 the following piece of code is illegal:


Params input parameters now with IEnumerable:

Now we can specify a variable number of arguments with IEnumerable and not only with array, like this:

Declaration expresions:

Normally we declare variable and then we assign it a value of an expression. C# 6 will allow us to join the declaration and the expression. Let me show you an example:


See how we declare out total inside the TryParse.
The benefit is that the code has become more explicit and that we have declare the variable where it makes more sense, taking more control over the scope.

Using static:

Simple but cool feature. If we have a static method on a static class, now we can declare the static class in the using part and just invoke the static method anywhere in the file. Therefore we don't have to type the typical Assert.AreEqual, but instead:

Conditional access:

Now instead of having lots of null checks before accessing properties of object, we can use ?. so we'll get the value of the property only if the object is not null, like this:


Expression bodied members:

This feature allows to assign value s of properties using Lambda syntax, like we have done here for NumberOfDishes:


There are a few more of features but this post is just a taste of what we will have soon available. It makes a bunch of small features but altogether it will allow to code simpler and more concise.
Remember, great power involves great responsibility.
Looking forward to have these tools in our daily projects!

Keep coding!

No hay comentarios:

Publicar un comentario