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>'

我的梦境 提交于 2019-12-05 08:45:54

string is already nullable, because it's a reference type. You don't need to wrap it in Nullable in order to have a null value. Not only is it not needed, but as per the error message you're getting, it's not even possible. Only non-nullable value types can be used as the generic argument for Nullable.

String Class is a class, it not a struct like System.Int32 or other primitive types. It can hold null value. Nullable<T> works with value types.

From the name it appears that you want to store DateTime object. Its always better to have DateTime in its own type ie. DateTime, for Nullable you can use Nullable<DateTime> or DateTime?

If you look at the docs on MSDN about Nullable<T> you will notice that T is constrained with struct. Constraints on Type parameters reveals that such a constraint restricts the generic parameter to being a value type except Nullable<T> (Do note that Nullable<T> is a struct!).

As MSDN docs say, string is a reference type meaning that the generic parameter constraint made for Nullable<T> will invalidate types such as Nullable<string> or with any reference type as generic parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!