Tuesday, May 24, 2011

How to Check Null Values in C#?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace NullableDataTypes
{
class Program
{
public static bool CheckNull(object obj)
{
bool _isNull = false;
if (obj is string)
{
//Only Checks For String value
}
else
{
int? X = obj as int?;
//OR int? X = (int?)arr[i];
//Note: X.HasValue returns false for reference type and Null values.
if (X.HasValue)
{
_isNull = false ;
}
else { _isNull = true; }
}
return
_isNull;
}
static void Main(string[] args)
{
bool isnull=Program.CheckNull
("");
Console.WriteLine("Is Supplied Value SPACE is null? -{0}", Program.CheckNull(" "));
Console.WriteLine("Is Supplied Value Bimlesh is Null? -{0}", Program.CheckNull("Bims"));
Console.WriteLine("Is Supplied Value NULL is null? -{0}", Program.CheckNull
(null));
Console.WriteLine("Is Supplied Value 1 is null? -{0}", Program.CheckNull
(1));
Console.
Read();
}
}
}

No comments: