Important C# Evolutions You May Not Know

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.   
  5. // If we want to use the Accelerate Methode  
  6. if(porsche!=null)  
  7.   porsche.Accelerate(10);  

With the safe navigation operator, we can do this.

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

Way to build a string (string interpolation)

  • There are so many ways to build string. The best and the faster one, if we want to talk about execution time, is the StringBuilder.
    1. // Create a StringBuilder that expects to hold 50 characters.  
    2. // Initialize the StringBuilder with "ABC".  
    3. StringBuilder sb = new StringBuilder("ABC", 50);  
    4.   
    5. // Append three characters (D, E, and F) to the end of the StringBuilder.  
    6. sb.Append("DEF");  
    7.   
    8. // Display the string  
    9. Console.WriteLine(sb.ToString());  
  • If we have some parameter to put in our string, we can use the String.Format like this.
    1. decimal temp = 20.4m;  
    2. string s = String.Format("The temperature is {0}°C.", temp);  
    3. Console.WriteLine(s);  
    4. // Displays 'The temperature is 20.4°C.'  

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

Your can see all the options on this link.

  • The evolution in C# 6 is the string interpolation new syntax,
    1. string name = Joe ;  
    2. string hours = "11:00";  
    3. Console.WriteLine($"Name = {name}, hours = {hours}");  

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.

  • Use the classic constructor - new
    1. Tuple<string, string,string> person = new Tuple<string, string,string>("Jhon","Doe","5555");  
    2. Console.WriteLine("Full Name: {0} {1}  ", person.Item1, person.Item2);  
    3. Console.WriteLine("PIN: {0} ", person.Item3);  
  • Use The static method - Create 
    1. // Create a 5-tuple.  
    2. var population = Tuple.Create("Tunisia", 6999, 54462, 9888, 5258456);  
    3. // Display the first and last elements.  
    4. Console.WriteLine("Population of {0} in 2013: {1:N0}", population.Item1, population.Item5);  
    5. // The example displays the following output:  
    6. //       Population of Tunisia in 2013: 5,258,456  
  • In C# 7, there is a new way to declare a Tuple and getting rid of the Item1, Item2, and name them with a significant keyword.
    1. public(string name, string location, string population) GetCountry()   
    2. {  
    3.     // fin a person in a list and return  
    4.     return ("Tunisia""North Africa""5258456");  
    5. }  
    6. var country = GetCountry();  
    7. Console.WriteLine("Population of {0} - {1} in 2013: {2:N0}", country.name, country.location, country.population);  
    8. // The example displays the following output:  
    9. //       Population of Tunisia - North Africa in 2013: 5,258,456  

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);