Since the announcement of the number one Microsoft language, C#, in 2000, a lot of developers have been interested in this language's evolutions for many reasons. The major one was that C# is intended to be a simple, modern, general-purpose, object-oriented programming language.

In 2007, Microsoft partners provided an open source implementation for the .NET framework called Mono which was used under Linux. However, it was not enough for Microsoft and in 2014, the company announced that the whole .NET stack ( compiler, cli, and clr etc. ...) is open source now and is available on GitHub.

This announcement ensured building larger .NET community and having a more vibrant, richer language. In this article, we will cover some C# evolutions you may not know and hopefully, they will help you with your projects and development.

Safe navigation operator (? operator)

The ? operator was introduced in .NET Framework 2.0 with the nullable types that allow variables to have null as a value or to don't have one at all, and we can test it with the HasValue property.

In C# 6, Microsoft added a new feature called "?" operator or safe navigation operator.

It's so simple and so useful. Let's see an example. Let's access a child property or method via the navigation operator "." but if you try to navigate into a null variable, you will have the famous NullReferenceException. So, we must test before accessing or calling any method on this object.

  1. // porsche is an instance of a Car Class
  2. if(porsche!=null && porsche.Name !=null)
  3. console.Write(porsche.Name); // we can use the Name proprety
  4. // If we want to use the Accelerate Methode
  5. if(porsche!=null)
  6. porsche.Accelerate(10);

With the safe navigation operator, we can do this.

  1. string nom = porsche?.Name ;
  2. // and to call Accelerate()
  3. porsche?.Accelerate();

Way to build a string (string interpolation)

In this method, there are many options to transform your string in a specific format.

Your can see all the options on this link.

An elegant way to structure an object (Tuple)

(This is just an example of use)

The majority of programming languages support functions and procedures with the standard format (a signature, parameters, and a return object). Generally, if we want to return many values or objects, we are obliged to use a list of objects or an array. To do otherwise, C# 6 offered us an elegant structure called Tuple.

MSDN defines a Tuple as

A data structure that has a specific number and sequence of elements. An example of a tuple is a data structure with three elements (known as a 3-tuple or triple) used to store an identifier such as a person's name in the first element, a year in the second element, and the person's income for that year in the third element.

There are many ways to create a Tuple.

An Enumerable Function's params

The params keyword gives you the possibility to have a variabale method's params number and pass a list of objects in the method call. It's a useful feature when you don't have any idea of the number of objects passed in the parameter.

  1. public void PrintValues(params string[] values)
  2. {
  3. foreach(var value in values)
  4. Console.WriteLine(value);
  5. }
  6. PintValues("Hello", "this", "is", "a", "test");
  7. // Consol print
  8. // Hello
  9. // this
  10. // is
  11. // a
  12. // test

Static Imports

With C# 6, you can import a dependecy with a keyword - static. Like this, you can use any method in the imported reference without specifying the class.

  1. using static System.Math;
  2. ...
  3. double x = 3;
  4. double y = 4;
  5. double distance = Sqrt(x * x + y * y);