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-07 05:23:43

问题


public virtual int Fill(DataSetservices.Jobs_detailsDataTable dataTable, 
    global::System.Nullable<global::System.String> fromdate,     
    global::System.Nullable<global::System.DateTime> todate)

I wrote above code in dataset.xsd in C#, but it is throwing an error:

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'

Suggest me how to use string because i want to use string and nothing else


回答1:


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.




回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/18852751/the-type-string-must-be-a-non-nullable-value-type-in-order-to-use-it-as-parame

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