??-Operator

C# contains a lot of little but very helpful treasures. One of these treasures is the ??-Operator. This operator may be used as syntactical shortcut for an if-statement to define a fall back value if the given value is null.

For example the statement a ?? b means: if value a is null then use value b, otherwise use value a.

 
Fall back values for outputs

The following source code contains two values which are set to null. These values will be written to the console window.

string stringValue = null;
Nullable<int> intValue = null;

Console.WriteLine("stringValue = " + stringValue);
Console.WriteLine("intValue = " + intValue);

 
The application creates the following output:

stringValue =

intValue =

 
In case of these null values the output is not very user friendly because no value will be shown. By using the ??-Operator, only a little modification is necessary to create a more user friendly output.

string stringValue = null;
Nullable<int> intValue = null;

Console.WriteLine("stringValue = " + (stringValue ?? "undefined"));
Console.WriteLine("intValue = " + (intValue ?? 0));

 
Now the application creates the following output:

stringValue = undefined

intValue = 0

 
Fall back values for calculations

In most cases calculations will result in an exception if one of the used values is null. For example the following source code will throw an InvalidOperationException in line four because the value is null.

Nullable<int> intValue = null;
int myValue;

myValue = intValue.Value + 10;

Console.WriteLine("myValue = " + myValue);

 
In this case it is also possible to use the ??-Operator. The following source code shows this little modification. This time line four contains a fall back value for the null case.

Nullable<int> intValue = null;
int myValue;

myValue = (intValue ?? 0) + 10;

Console.WriteLine("myValue = " + myValue);

 
Chained ??-Operators

It is also possible to create a chain of ??-Operators. The following example shows three variables. Each variable is null and therefore the last fall back value undefined is used.

string valueA = null;
string valueB = null;
string valueC = null;

string value = valueA ?? valueB ?? valueC ?? "undefined";

Console.WriteLine("value = " + value);

 
Summary

The ??-Operator offers an easy way it to define a fall back value in case a value is null. Of course it is also possible to use this operator in more complex statements. But in such cases I would recommend to use an if-else statement instead of the ??-Operator because in more complex statements this may increase the readability of the source code.

Advertisements
Dieser Beitrag wurde unter .NET, C# abgelegt und mit , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s