rulururu

post More fun with the “?” operator

January 7th, 2008

Filed under: .NET — Kai @ 9:06 am

As I wrote in my last post you can use the “?” operator for Nullable types in .Net 2.0 but there’s one more feature:
To abbreviate a progess like that (which is very long winded)

string assign(string v)
{
string r;
if( v == null )
r = "null"
else
r = v;
return r;
}

I’d rather write a function that way:

string assign(string v)
{
return ( v == null ) ? "null" : v;
}

But the shortest and language adjusted way seems to be:

string assign(string v)
{
return v ?? "null";
}

Even if it seems cool to me I don’t think it’s the most readable way. Anyway, have fun with it ;)

1 Comment »

  1. […] recently wrote about the coalescing operator and its usage, now I’d like to show you a really effective way to use in with […]

    Pingback by Kai’s Blog » Null coalescing operator and LINQ — January 24, 2008 @ 9:40 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)