Using value types

There are three general value types

1. Built in types
2. User defined types
3. Enumeration

All the above value types are defined as a struct.
Since, they are not reference variables, we cannot assign null as the value.
If you want a value type can be assigned a null value then define the variable as
System.Nullable<T> where T is the type of the variable

For example System.Nullable<bool> flag = null;
so the flag variable can now be assigned either true, false or a null value.

The shortcut for the above statement is
bool? flag = null;
bool? b1,b2;
b1 = b2 = null;

If a variable is declared as nullable (Nullable is also a struct) it gets an additional property HasValue, which can be used to check if values is null or not
if(b1.HasValue)
System.Console.WriteLine(sizeof(System.Boolean)); return 1 byte.

TODO:
struct test
{
int i;
}
System.Console.WriteLine(sizeof(test));
This gives CTE.
How can my struct return the sizeof value

Why not
int? i = 10;
if (i==null)
System.Console.WriteLine(i);
else
System.Console.WriteLine(“no value”);

Advertisements

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


%d bloggers like this: