Home » Advanced CSharp » CSharp ?? Operator

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

Related Post

  • CSharp Numeric OverflowsCSharp Numeric Overflows Did you know that when you are dealing with numbers, by default, .NET will do, or try to do exactly what you tell it to do?  If you tell it to do the impossible, it will do the next […]
  • Using VB.NET From CSharpUsing VB.NET From CSharp Back when I was teaching .NET to other programmers regularly, I would frequently get the following question from a VB6 programmer moving to CSharp. “Is there any way of using some of the […]
  • CSharp’s Property ShortcutsCSharp’s Property Shortcuts There are a lot of nice shortcuts in the CSharp language that most of us never use.  But if you take the time to learn them, you can be as productive as a student I had who had learned all […]
  • .Net String Pool – Not Just For The Compiler.Net String Pool – Not Just For The Compiler On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are […]
  • Removing Warnings from CSharp Compile CycleRemoving Warnings from CSharp Compile Cycle One of the things I try to do when I'm working on my CSharp projects is to make sure I don't have any compile errors or warnings.  The reason for removing the errors is probably […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

  • http://www.akasse.org/ A kasse

    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.

    • Dave

      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?