简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM