What is nullable type in c#?
when we have to use nullable type in C#.net? could any one please explain with example.
Nullable types (When to use nullable types) are value types that can take null as value. Its default is null
meaning you did not assign value to it. Example of value types are int, float, double, DateTime, etc. These types have these defaults
int x = 0;
DateTime d = DateTime.MinValue;
float y = 0;
For Nullable alternatives, the defualt of any of the above is null
int? x = null; //no value
DateTime? d = null; //no value
This makes them behave like reference types e.g. object, string
string s = null;
object o = null;
They are very useful when dealing with values from database, when values returned from your table is NULL
. Imagine an integer value in your database table that could be NULL, such can only be represented with 0
if the c# variable is not nullable - regular integer.
Also, imagine an EndDate
column whose value is not determined until an actual time in future. That could be set to NULL in the DB but you'll need a nullable type to store that in C#
DateTime StartDate = DateTime.Today;
DateTime EndDate? = null; //we don't know yet
When we have to use nullable type in C#.net?
Imagine there is an integer variable id
that represents a particular id
.
You can store 1
,44
,or anything else.But what if you don't know the id.You cant just store -1
or 0
.You may be thinking to assign null
but normally null
cannot be assigned to value types.
int id=null;//error
Nullable type
enable you to do this.
Value types like int,double,char..
cannot be represented as NULL
value.
To represent null
in value type you must use nullable type
..
It is denoted as a value type followed by ?
int? id=null;
which is transalted to
Nullable<int> id=new Nullable<int>();
The default value of it a nullable type is null
.
Nullable type is new concept introduced in C#2.0 which allow user to assingn null value to primitive data types of C# language. Important to not here is Nullable type is Structure type.
BlogPost : Nullable type -- Why we need Nullable types in programming language ?
from: http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80).aspx Nullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value. For example, a Nullable<Int32>, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable<bool> can be assigned the values true or false, or null
class NullableExample
{
static void Main()
{
int? num = null;
if (num.HasValue == true)
{
System.Console.WriteLine("num = " + num.Value);
}
else
{
System.Console.WriteLine("num = Null");
}
//y is set to zero
int y = num.GetValueOrDefault();
// num.Value throws an InvalidOperationException if num.HasValue is false
try
{
y = num.Value;
}
catch (System.InvalidOperationException e)
{
System.Console.WriteLine(e.Message);
}
}
}