I was looking through some code the other day and ran across something that looked remotely familiar. It was the null coalescing (??) operator. I had read about this operator, but never used it. It was introduced with the .NET 2.0 Framework.
The null coalescing operator basically checks to see if a value is null and if so returns an alternate value. Below is a simple example.
View the C# code sample [HERE]
Results

–
Related Content
- C# Conditional Operator ( ?: ) and Null-Coalescing Operator ( ?? ) – David Hayden
- The C# ?? null coalescing operator (and using it with LINQ) – Scott Guthrie
- ?? operator (C#) – DotNet Tip of the Day
–
“I am learning all the time. The tombstone will be my diploma.” -Eartha Kitt
[...] The C# Null Coalescing Operator (??) (Rhonda Tipton) [...]
By: Dew Drop – September 28, 2008 | Alvin Ashcraft's Morning Dew on September 28, 2008
at 6:52 am
Heard of it to
C++ has a similar thing just it has to option for true and false
greetings
By: patryksharks321 on September 28, 2008
at 12:00 pm
@Patryksharks321: You’re talking about the conditional operator (?:) which C# also has.
I abuse this operator in “singleton-like” properties…
e.g.
private object myObject = null;
public object MyObject
{
get { return myObject ?? ( myObject = new object ( ) ); }
}
By: ins0mniaque on September 29, 2008
at 10:50 am