?? Operator and its use case

Long time I asked myself, for what the heck I could use the ?? operator. Last days I found the perfect scenario for using this operator.

Think about accessing your application config file using the configuration manager. This could look like:

string value = ConfigurationManager.AppSettings["AppKey"];
if (value == null)
    value = "DefaultValue";

If you want to do it a little bit more elegant, you could use this code:

string value = ConfigurationManager.AppSettings["AppKey"];
value = (value != null) ? value : "DefaultValue";

Or you say:

string value = ConfigurationManager.AppSettings["AppKey"] ?? "DefaultValue";

I think, that’s Impressive! If you have some other use cases for this operator I would like to know them.

Cheers
- Gerhard

kick it on DotNetKicks.com

About these ads

8 Responses to “?? Operator and its use case”

  1. J Says:

    It’s its, not it’s

  2. Kris van der Mast Says:

    Hi,

    recently I also wrote several articles about the null coalescing operator: http://blog.krisvandermast.com/SearchView.aspx?q=%22null%20coalescing%20operator%22.

    Since I found out about the existence of this incredible usefull operator I used on all possible occasions. Too bad VB.NET doesn’t support an equivalent of it.

    Grz, Kris.

  3. Justin Etheredge Says:

    In VB.net you can kinda fake it with a generic method, but the C# operator is just so much more elegant.

  4. Daniel Says:

    I couldn’t understand some parts of this article nnial 2007 – salvatore iaconesi – del.icio.us poetry, but I guess I just need to check some more resources regarding this, because it sounds interesting.

  5. Bryan Migliorisi Says:

    This was my absolute favorite change when moving from VB to C#

  6. Dave Langer Says:

    great! I wa using something stupid like this:

    int val;
    int.TryParse(ds.Tables[0].ds.Tables[1].Rows[0]["PAY_MONTH"].ToString().Trim(), out val);
    payMonth = val;

    and now:
    payMonth = ds.Tables[1].Rows[0]["MES124"].ToString().Trim() ?? 0;

    Thanks Gerhard, 3rd time I found something useful here, keep the good coding…

  7. Ben Gotow Says:

    That’s pretty cool – I’ve never really understood the use of the ? and ?? operators. Between that and LINQ – I can probably clean up my C# code :-D

  8. Mike Says:

    You can also string ?? operators together which makes it even more useful sometimes, e.g.:

    string value = ConfigurationManager.AppSettings["AppKey"] ?? ConfigurationManager.AppSettings["UserKey"] ?? “DefaultValue”;

    This allows you to prioritise assignment of values and use the first non-null or the default if all are null.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 106 other followers

%d bloggers like this: