Little-known C# 2.0 operator – ??

August 21, 2007

Excerpt from the msdn:

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

Example:

string s = null;
int? i = null;
Console.WriteLine(s ?? "(null)");
Console.WriteLine(i ?? -1);

Will produce:

(null)
-1

Entry Filed under: .net, c#, tip. .

2 Comments Add your own

  • 1. leppie  |  August 21, 2007 at 2:41 pm

    Very handy for default values :)

  • 2. gramotei  |  August 21, 2007 at 3:06 pm

    exactly:)

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

August 2007
M T W T F S S
    Sep »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Most Recent Posts