Nullable Type In C# with Example
IN THIS POST WE WILL LEARN
1. NULLABLE TYPE IN C#
2. NULL COALESCING OPERATOR ??
Type in C#
In C# types are divided into 2 broad Categories
Reference Types : Interface , Class , Delegates , Arrays E.t.c
Value Types : Int , Float , Double , Structs ,Enums E.t.c
By Default value types are non nullable. To make then nullable Use ?
int r= 0 (r is non nullable, so r cannot be set null , r= null will generate Error )
int? m =0 (m is nullable int , so m=null is legal)
Example : Nullable Type In C#
- using System;
- namespace NullableTypeInCshape
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool? AreYouvirgin = true;
- if (AreYouvirgin ==true)
- {
- Console.WriteLine("Yes I am virgin ");
- }
- else if (AreYouvirgin == false)
- {
- Console.WriteLine("Yes I am Not virgin ");
- }
- else
- {
- Console.WriteLine(" Use Can Not Given Answer");
- }
- Console.ReadLine();
- }
- }
- }
2. NULL COALESCING OPERATOR ??
Example : With Out Null Coalescing Operator
Example : Using Null Coalescing Operator
Output:
Comments
Post a Comment