.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Nullable Types - at a spark

Posted By:Gaurav Arora       Posted Date: April 27, 2009    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  
 

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 : http://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:

  1. Open Console Window of available Visual Studio.
  2. Start - > Programs -> Visual Studio [version] ->Visual Studio Tools - >Console
  3. 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

 


 



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend