C# Nullable Types

Acccording to MSDN, there were about 24 types of nullable in C# 3.0, but only 3 types are drafted. They are

Using Nullable Types (C# Programming Guide)
Boxing Nullable Types (C# Programming Guide)
?? Operator (C# Reference)
Let me draft about the point that lead me to post this information.

With in our application, we want to validate a variable towards null. we started validating the variable with the ".HasValue" property. During some progress, suddenly some thing flashed in my mind about my experience of working with the teritory operator (of C or perhaps VB). The syntax could be some thing below

variableX = (condition) ? truevalue : falsevalue

similar such, is there in C#, not similar thou, but approxmately, ?? operator returns the left-hand operand if it is not null or it returns the right operand. You can observe from the below codesnippet



int? p = null;

int? y = 7;

if (p.HasValue)

y = 9;

int c = p ?? y; // this will return 7 as the value in p is null

Console.WriteLine(" \n The value of c is {0} ", c);

-----------------------
If you have something to share in person on this post, pl drop me a mail at dskcheck@gmail.com with the title in the subject.

Comments

Popular posts from this blog

Network Intrusion Detection using Supervised ML technique

Common mistakes by Interviewer

Keep the system active, to avoid the auto lock