How to create structure with null value support?

Hamed picture Hamed · Jul 3, 2011 · Viewed 14k times · Source

I'm new in C#. In c# I can't set value of a structure to null how can I create a structure with null value support?

Answer

MrWednesday picture MrWednesday · Jul 3, 2011

Structs and value types can be made nullable by using the Generic Nullable<> class to wrap it. For instance:

Nullable<int> num1 = null;

C# provides a language feature for this by adding a question mark after the type:

int? num1 = null;

Same should work for any value type including structs.

MSDN Explanation: Nullable Types (c#)