I have a class (used by filehelpers) which gives me an error when I try to define a nullable string:
public String? ItemNum;
The error is:
Error 1 The type 'string' must be a non-nullable value type in order
to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
This occurs even with the lowercase string, though I haven’t yet seen a difference between those.
Using another type such as int, decimal etc is fine:
public decimal? ItemNum;
Some general looking on the net talks about defining constructors by field etc, but given the other fields work fine, what’s special about string? Is there an elegant way to avoid it?
stringis reference type, reference types are nullable in their nature.When you define
public string ItemNum, it is already nullable.Nullablestruct was added to allow make value types nullable too.When you declare
public decimal? ItemNum, it is equivalent topublic Nullable<decimal> ItemNum.Nullablestruct has definition:where T : structmeans thatTcan be only value type.Description in MSDN is very detailed Nullable Structure.
Quote: