Blog Archives

C# 6.0 – Elvis operator and other improvements


Microsoft has released yet another version of .Net framework (4.6) and C# (6.0). In this post I am highlighting few improvements released as part of C# 6.0 which will largely benefit day to day development activities of a C# developer.

Elvis operator or formally called null conditional operator (?.) makes the null check simple. In a highly hierarchical object structure this operator reduces the length of code required to check for null and proceed further.

public class Sale
public Order orderItem { get; set; } = new Order();
public class Order
public int orderId { get; } = 101;

To read the value of orderId from main() class, null checks are required on objects of two of its hierarchical structure.

Sale sale = new Sale();

if(sale != null && sale.orderItem !=null && sale.orderItem.orderId > 0)
Console.WriteLine(string.Format("In older C#, value is {0}", sale.orderItem.orderId));

if (sale?.orderItem?.orderId >0)
Console.WriteLine($"In 6.0 C#, value is {sale.orderItem.orderId}");

Elvis operator makes this simple by evaluating the above expression left to right. The expression sale?.orderItem?.orderId >0 evaluates to orderItem if the left operand sale is non-null; otherwise, it evaluates to null.

In the above class definition, you will observe the way I have initialized the properties. I have directly initialized orderItem next to the property definition and it’s called auto-property initializer. Also you will notice orderItem has only get and no setter, this feature is called get-only auto property.

Also in the above code block you will notice, first I used string.Format to format the string and latter a special and simplified template to do the same. This is again a new feature in C# 6.0 called Interpolated Strings. An interpolated string expression creates a string by replacing the contained expressions with the ToString representations of the expressions’ results.


Handle optional parameterized method of VB.Net or COM in C#

Although optional parameters are actually a very good feature, C# does not support them. Now what about assemblies created in VB.Net with methods having optional parameter, how to call them from C# code? Since C# doesn’t support the concept of optional parameter, if you don’t supply required parameters while making a call to such methods, compiler will throw an error.

So how to handle this? There are many ways to address this issue:
1. If the required parameter is of any reference type such as String then you can use
System.Reflection.Missing.Value or  System.Text.Missing
While making call to such methods pass the above value as optional parameter.
2. Create a Wrapper class in VB.Net and overload the method with different available options. This assembly could be referenced by the original C# application.

Now the upcoming version of C# 4.0 is going to have a feature of Optional Parameter
C# 4.0 can both declare and consume optional parameters. Here’s a sample of a very simple method that declares a parameter as optional:

public static class OptionalDemoLib
public static void SayHello(string s = "Hello World!")

This means you can either call Do with one argument or without an argument, in which case the default value is used:

public static class OptionalDemo
public static void Main()
OptionalDemoLib.SayHello("Hello India!");