Nullables in C#
Nullables in C#
Defiition:
Nullable types are instances if System.Nullable. A nullable type can represent the normal range of values of its underlying value type plus an additional null value.
Purpose:
The purpose of this code-snippet to describe the use of nullable types with little different of a general type.
Following Console code-block tells the all above :
/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : msdotnetheaven.com
* File Name: nullable.cs */
using System;
using System.Collections.Generic;
using System.Text;
namespace AStepAhead.Nullable
{
class nullableclass
{
static void Main(string[] args)
{
int? num = null;
int? num1 = null;
if (num.HasValue == true)
{
Console.WriteLine("Num : {0}", num);
}
else
{
Console.WriteLine("Num has Null value");
}
//int y = num.GetValueOrDefault(); //throw an exception
int z;
try
{
//y = num.Value;
//Console.WriteLine(“Y:{0}”, y);
z = num1 ?? 2;
Console.WriteLine(“Z:{0}”, z);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
Steps to test above:
- Open Console Window of available Visual Studio.
- Start – > Programs -> Visual Studio [version] ->Visual Studio Tools – >Console
- Now compile the abovefrom console as follow:
/>csc .exe nullableclass.cs
Above will produce an executable file nullableclass.exe, double click on this file.
Note :
You can also verify to uncomment above commented lines.
Popularity: unranked
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.










Leave your response!
You must be logged in to post a comment.