C# null Operator check ??

4/1/2014·1 min read

C# had provide (??) operator to check NULL value, be honestly, i don't think it help something different. Either from readability and productive.

C# -- if we don't use (??) to check NULL value

public string CheckCondition(int[] values)

{
if (values == null)
{
return "value is null.";
}
else
{
return "value is not null";
}
}

 

public string CheckCondition(int? value)

{
if (value == null)
{
return "value is null.";
}
else
{
return "value is not null";
}
}

 

C# -- use (??) to check NULL value

public string CheckCondition(int[] values)

{
var t = values ?? "value is null."
}

 

It'll be cool to use (??), since it's kind of cool to make two question mark(??). But I'd rather use if else. If Microsoft could prove something like (?? :), which is "if it's null(??) do this, else do others.

Operators ?? also can't be overloaded. So, if you want to try something like How to OverLoading Operator.This. Which you can't do it for (??).