Basic usage

Download c# eBook

Example

Using the null-coalescing operator (??) allows you to specify a default value for a nullable type if the left-hand operand is null.

string testString = null;
Console.WriteLine("The specified string is - " + (testString ?? "not provided"));

Live Demo on .NET Fiddle

This is logically equivalent to:

string testString = null;
if (testString == null)
{
    Console.WriteLine("The specified string is - not provided");
}
else
{
    Console.WriteLine("The specified string is - " + testString);
}

or using the ternary operator (?:) operator:

string testString = null;
Console.WriteLine("The specified string is - " + (testString == null ? "not provided" : testString));

Stats

Contributors: 14
2017-03-28
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook