OLAP

We declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but .net makes us intelligent with one of their operator, ‘?’

nullable

Nullable is represented by a ‘?’.

Prior to that, uninitialized variables in C# is not allowed so we should know default values of .net datatypes.

For value types, mostly it is 0. For ex:
int id=0;

I have specified only the basic value types which we use in our daily development work. For detailed list for value type, click here.

Value type Default value
bool false
byte 0
char
int 0
long 0L
struct The value produced by setting all value-type fields to their default values and all reference-type fields to null.

For reference type, default value is null.

Now, the condition we normally see is to have default value as ‘Null’ for value type. To achieve this we have nullable operator ‘?’ which we mentioned earlier. How to use it?

Just put it in front of your value type variable. For ex:

int? id=null;

This will make int variable accept ‘Null’. Please note this one more important information, if we have one int variable and other nullable int variable then they are not equivalent and cannot be assigned to each other. For ex:

int x=1;
int? y=2;
x=y; — It is not possible and will throw compile time error.

To correct it, Both the variables should be nullable.

Nullable has two more properties. ‘HasValue’ for a null condition check and ‘Value’ to get the actual value.

We sometimes fail to differentiate between the uses of ‘?’ as it can be used in many ways.To avoid the confusion regarding ‘?’. One of the use is a conditional operator. For ex:

int id = x > y ? x : y;

it is same as -

int id;
if( x > y )
{
id= x;
}
else
{
id= y;
}

Same kind of operation can be written with ‘??’ but only for null check.

int? x=1;
int? y= x?? 1;

This exercise requires some real time practice and will be much more clear so I request you all to try it and send your feedback. .

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • C#: My experience with const keywordC#: My experience with const keywordThere are numerous post on internet about 'what is const keyword in .net' or 'what is the difference between const keyword and read only' and I must say many might have already mugged it up like me...
  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • Asp.Net: Hidden Truth of CookiesAsp.Net: Hidden Truth of CookiesI know there are several articles on cookies as what are they? and how can they be created? but i always wanted to know more about them. My questions were where they are stored? how to track their ...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • Why Multiple Inheritance is discouraged?Why Multiple Inheritance is discouraged?Hi Guys, This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance? Most of you guys already know that multiple inheritance is not available for...
  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...
  • VB.Net: Download file from FTP and UnZipVB.Net: Download file from FTP and UnZipExperience: Many application requires data migration tasks to execute on daily basis. To achieve this we generally seek help of SSIS package, if at all we are making use of SQL sever. ...