Introduction:
In this article,i am going to explain about how to Use the Null-Coalescing Operator (??) in C# 4.0.
Main:
If You want to simplify checking for null values Null-Coalescing Operator (??) is the simple and best option.For ex This is a common situation, where you need to check for a null value before you use a
variable.
int? n = null;
object obj = “Hello”;
int x = 13;
//short for if (n!=null) o = n; else o = -1;
int? o = n ?? -1;
object obj2 = obj ?? “ok”;
//doesn’t make sense since x can’t be null
//int y = x ?? -1;
Console.WriteLine(“o = {0}”, o);
Console.WriteLine(“obj2 = {0}”, obj2);
The output is as follows:
o = -1
obj2 = Hello
int? n = null; object obj = “Hello”; int x = 13; //short for if (n!=null) o = n; else o = -1; int? o = n ?? -1; object obj2 = obj ?? “ok”; //doesn’t make sense since x can’t be null //int y = x ?? -1; Console.WriteLine(“o = {0}”, o); Console.WriteLine(“obj2 = {0}”, obj2); The output is as follows: o = -1 obj2 = Hello |
Conclusion:
Hope this helps,
Happy Coding.
nice post. thanks.
have a look at this link
http://aspdotnethacker.blogspot.com/2010/04/c-null-coalescing-operator.html
Would you mind if I use some of the info here, and I’ll leave a link back to you?
Web Development has always been a passion of mine. I’m always trying to learn more and get better. Thanks.
Being a complete newbie, all I can say is thanks for sharing this.
Very of use in turn and i bear found it pro a slow period,appreciation so much.
A good post indeed! That’s the sunny side of your writing, you write in a lucid manner and I have no difficulty to understand what you have said, even though I am a novice. Keep the good work going by continue blogging new and entertaining posts. I have already subscribed to the RSS feed of your weblog and look forward to reading more of your blog posts in the future.
A good post indeed! That’s the sunny side of your writing, you write in a lucid manner and I have no difficulty to understand what you have said, even though I am a novice. Keep the good work going by continue blogging new and entertaining posts. I have already subscribed to the RSS feed of your weblog and look forward to reading more of your blog posts in the future.
I read your blog,you bLog very useful me,I look mark you blog! Thanks. Good nice.
You’ve actually a attention individual posting design and present very info. The articles are good to understand and you don´t use wonderful deal of ads like all the other bloggers.