New Improvements in C#6

In this article I would like to mention some new functionalities that became available with a launch of VS 2015. At the same time as new VS version was launched the Microsoft also launched the new version of c# which was included in the new build (version 14).

Experts working with .Net Development Company are explaining new enhancements made in C#6 via this article post. If you are a .net developer and want to upgrade your skills, this post is for you. Read the complete article and brush up your C#6 skills.

For some the changes in the new version of c# may be of little value, because they are very few and difficult for junior developers. But for the old .NET users these improvements will save a lot of time and provide much more flexibility in the day to day development.

Also a big effort was given to the new compiler for the .NET which is “Roslyn”.

Strings interpolation
In the new c# is becomes possible to interpolate strings the same way as in the ECMAscript 6, on my opinion these technologies each time are getting closer. Basically you will be able now to insert text directly in the string. The very same way as with string.Format() method, but now the work will be done natively in a very optimized way. Let’s take a look at both examples:

<code> string test = string.Format("my name is {0}", p.Name); </code> <code> string test = $"my name is {p.Name}"; </code>

So on my opinion this will be very useful feature and will save many .NET applications from errors which may be generated using string.Format() method.

Usage of expressions in Method members

First of all, I would like to mention the new way of using lambda expressions in methods parameters and in they scope. With the new version of language is now possible to use expression very similar to lambda expressions in order to define methods, with only one statement or in one block.

Methods could be declared by developer defining its body with using of lambda arrow:

<code> public Coordinates Go(int a, int b) => new Coordinates(x + a, y + d); </code> <code> public static Special operator +(Special x, Special y) => x.Add(y); </code> The result is the same as if methods had only return instruction. The examples above are converted by the compiler into following code: <code> public Coordinates Go(int a, int b){ return new Coordinates(x + a, y + d); } </code> <code> public static Special operator +(Special x, Special y){ return x.Add(y); } </code>

In the cases when methods doesn’t have return (void type) these functionality still be valid, but the expression have to be built in a way of an instruction:

<code> public void Digitalize() => Console.WriteLine("Digitalizing" + Data); </code> The expression body format could also be used to define properties and read-only indexers: <code> public string Description => Operation + " " + Date; </code> <code> public Car this[int idNumber] => stock.LookupStock(idNumber); </code> New way to reference libraries
The same as happening with “using” (importing external libraries) for namespaces, the “using static” can reference static members of a class or enums, enabling using such members without specifying the class name.

<code> using static System.Console; class TestProgram { static void Main() { WriteLine(“Using static is working properly”); } } </code>

Let’s suppose that you are working on a big logic file (more than 2000 lines of code) with several methods, and in all of that methods you using the methods provided by the same external library, so you could save a lot of code and also reduce a complexity for those who going to read your code if you could not specify a class names. Like the Console in the example above.

properties in the new fashion
Properties are called “auto properties” when they are implemented automatically which means that they are not abstract and not external. The accessors for that properties are defined using semicolon.

When a property is implemented automatically, the hidden field is created that gives support to the property and read/write accessors, which is implemented to read and write that field.

In the new version of the language the initialization of properties declared automatically could be done in the same fashion as the fields are initialized:

<code> public class Car { public string Model { get; set; } = "Sedan"; } </code>

When initializing properties like in the example above the initializer will fill directly the value into the field, without using setter for of the property.

Also one very good improvement is that now is become possible to declare read-only properties hiding the accessor responsible for writing, the setter. Let’s take a look at the following example:

<code> public class Car { public string Model { get; } = "Sedan"; } </code>

After the compiler is passed thorough the code above it will generate a read-only field implicitly.

The other way to initialize read-only properties is using its constructor. Let’s take a look at the following example:

<code> public class Car { public Car(string model) { Model = model; } } </code>

So in that sense we automatically created the read only Model field which only may be assigned through the constructor.

Null-conditional operator
Now is becomes possible to reduce the quantity of the code we are writing when checking for possibility of our variables having null value. With the introduction of the null conditional operator we may put “?” before the invocation of a method from any possibly null instance of some class, and the .NET will deal for us with checking the instance of being null and will return null in the case when its happen.

<code> // will assign null if client is null long? clientId = client?.Id; // will assign null value if myStringList is null string second = myStringList?[1]; </code>

After the work of compiler, the code is translated in the following statement:

<code> long? clientId = (client != null) ? new long?(client.Id) : null; string second = (myStringList != null) ? myStringList [0] : null; </code>

Also one very usefull way to apply the null-conditional operator is to combine it with the coalesce operator:

<code> long clientId = client?.Id ?? 0; </code>

So, basically in the example above if the client is null the default value to assign will be 0.

Developers of .Net development Company have just shared a great post about major enhancements introduced by officials of C#6. You can share your development experience with other readers on this post through comments.


Client Testimonials

  • Fabio Durso

    We found a reliable and efficient partner in NexSoftSys, ready to support our strategy of offshore software development. NexSoftSys has particularly impressed by the speed of response to any request for the development of software applications and for their maintenance.

  • Filipe

    We did hire full time Java developers from NexSoftSys, to help us to improve a time to market of a product. The software platform is based on Java & Extjs, and they are delivering the software on time and with success. We strongly recommend NexSoftSys as Reliable Development partner.

  • Steve

    Powerful solutions are given by NexSoftSys dedicated developers for my projects. They suggest solutions as per current market trend. Other than this, the team is always ready for any type of changes or update. That is the main reason that I would like to give my next project to them.

Copyright © 2016 - NexSoftSys All rights reserved