C# – Making a Non-nullable value type nullable

.net-3.5cnon-nullable

I have a simple struct that has limited use. The struct is created in a method that calls the data from the database. If there is no data returned from the database I want to be able to return a null, but Visual Studio complains, Cannot convert null to PackageName.StructName because it is a non-nullable value type.

How can I make it nullable?

Best Answer

You want to look into the Nullable<T> value type.