In C# Types are divided in 2 Categories:
1. Value Types:-int, float, double,structs, enums etc
2. Reference Types:-array, class, interface, delegate etc
By default value types are non nullable data type. We use ? mark for make them nullable
int a = 0(a is non nullable , so i can't set it to null, it generate error)
int? b=0(b is nullable , so b=null is legal)
Program Using Nullable type :
using System;
class Program
{
public static void Main()
{
int AvailableChocolate;
int? ChocolateForSale = null;
if (ChocolateForSale == null)
{
AvailableChocolate = 0;
}
else
{
AvailableChocolate = (int)ChocolateForSale;
}
Console.WriteLine("Available Chocolate = {0}",AvailableChocolate);
}
}
1. Value Types:-int, float, double,structs, enums etc
2. Reference Types:-array, class, interface, delegate etc
By default value types are non nullable data type. We use ? mark for make them nullable
int a = 0(a is non nullable , so i can't set it to null, it generate error)
int? b=0(b is nullable , so b=null is legal)
Program Using Nullable type :
using System;
class Program
{
public static void Main()
{
int AvailableChocolate;
int? ChocolateForSale = null;
if (ChocolateForSale == null)
{
AvailableChocolate = 0;
}
else
{
AvailableChocolate = (int)ChocolateForSale;
}
Console.WriteLine("Available Chocolate = {0}",AvailableChocolate);
}
}
No comments:
Post a Comment