CSharp ?? Operator
One of the problems with learning a language early in its life cycle is that by the time version three comes out, you never look at or implement any of the new features.
There are two reasons for this. First, the features you are using already cover 80% of the features you need. Second, you never look at the manual to discover what new features are available–features that may help you write your code more efficiently.
The CSharp ?? operator is one such feature.
We’ve all written code that looks something like this:
if (s == null) s = "some default value";
Since version one, we could write this with less text by saying
s = s == null ? "some default value" : s;
But did you know you could write that same code in even less code using the double question mark operator?
s = s ?? "some default value";
There are some who might say that each shortened form makes the code less readable. For those people I say, once you know the syntax it is no more unreadable than any other code we might write.
Other post in Advanced CSharp
- Two Interfaces. Same Method. Two meanings. - September 29th, 2008
- Making values nullable - October 9th, 2008
- CSharp's Property Shortcuts - October 23rd, 2008
- Readonly variables in CSharp? Really?! - October 29th, 2008
- Dispose with Using - November 10th, 2008
- Delegates in .NET - December 4th, 2008
- Using Sealed in CSharp - December 8th, 2008
- CSharp checked and unchecked - December 11th, 2008
- Advanced CSharp - unsafe mode - December 15th, 2008
- Volatile variables and CSharp threads - December 22nd, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- CSharp fixed keyword - January 5th, 2009
- using - There's more there than you are using - February 2nd, 2009
- Stackalloc in CSharp - February 16th, 2009
- Removing Warnings from CSharp Compile Cycle - March 10th, 2009
- && vs & and | vs ||... What's the difference? - March 16th, 2009
- Advanced CSharp - yield - March 25th, 2009
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- CSharp ?? Operator - May 18th, 2009
- Using VB.NET From CSharp - July 1st, 2009
- Dispose, Finalize and SuppressFinalize - July 9th, 2009
- What is .NET’s Object.GetHashCode() Used For? - August 5th, 2009
- ASP.NET Substitution Control - October 22nd, 2009
Other Related Items:
Chopin: MazurkasPillow - Roll (Scribbles)
LINQ in ActionLLINQ, Language INtegrated Query, is a new extension to the Visual Basic and C# programming languages designed to simplify data queries and databas... Read More >
If you're new here, you may want to subscribe to the mailing list to get notifications of new post and a virtual tour of past topics. Thanks for visiting!










I think it will be better if you declare your variable using function, there it might much easier to call the variable when the condition needs it.
I’m sorry, I’m not following your logic.
You are going to call a function to do what this does in one line so it is easier to call?
What makes it easier to call?