Little-known C# 2.0 operator – ??

August 21, 2007 at 2:39 pm 2 comments

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
Advertisements

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

Links For Continuous Learners New Lightweight Dependency Injection Framework for .NET – Ninject

2 Comments Add your own

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

    Very handy for default values 🙂

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

    exactly:)

    Reply

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

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


%d bloggers like this: