How to declare user defined Nullable type in C#

  1. using System.IO;  
  2. using System;  
  3. struct MyStruct // Declare a struct.  
  4. {  
  5.     public int X; // Field  
  6.     public int Y; // Field  
  7.     public MyStruct(int xVal, int yVal) // Constructor  
  8.     {  
  9.         X = xVal;  
  10.         Y = yVal;  
  11.     }  
  12. }  
  13. class NullableType   
  14. {  
  15.     static void Main(string[] args)   
  16.     {  
  17.         MyStruct mSStruct = new MyStruct(6, 11); // Variable of struct  
  18.         MyStruct ? mSNull = new MyStruct(5, 10); // Variable of nullable type  
  19.         Console.WriteLine("mSStruct.X: {0}", mSStruct.X);  
  20.         Console.WriteLine("mSStruct.Y: {0}", mSStruct.Y);  
  21.         Console.WriteLine("mSNull.X: {0}", mSNull.Value.X);  
  22.         Console.WriteLine("mSNull.Y: {0}", mSNull.Value.Y);  
  23.     }  
  24. }  
Output:

mSStruct.X: 6                                                                                                                                                                   
mSStruct.Y: 11                                                                                                                                                                  
mSNull.X: 5                                                                                                                                                                     
mSNull.Y: 10