This example show how to use the followings
  • ? operator
  • HasBalue
  • set int as null

// csc operator_nulltype.cs
//?? opertators
using System;
namespace ex_null_opertaror
{
class Null_class
{
public static int? GetNullableInt()
{
return null;
}
public static string GetStringValue()
{
return null;
}
}
class test
{
static void Main()
{
// ?? operator example.
Console.Write(" Enter a Nuber " );
int n1 = int.Parse(Console.ReadLine());
int? x = null;
int y = x ?? n1;
Console.WriteLine("Set y as int y = x ?? :" + y );
// Assign i to return value of method, unless
// return value is null, in which case assign
// default value of int to i.
int i = Null_class.GetNullableInt() ?? default(int);
if (x.HasValue==false) {Console.WriteLine("Value of i : " + y.ToString());}
string s = Null_class.GetStringValue();


// ?? also works with reference types.
// Display contents of s, unless s is null,
// in which case display "Unspecified".
Console.WriteLine(s ?? "Unspecified");
Console.ReadLine();
}
}
}//end of the namespace