简体   繁体   English

类型'string'必须是不可为空的值类型,以便在泛型类型或方法'System.Nullable <T>'中将其用作参数'T'

[英]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: 我在C#中的dataset.xsd中编写了上面的代码,但它抛出了一个错误:

Error 1 错误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' 类型'string'必须是非可空值类型才能在泛型类型或方法'System.Nullable'中将其用作参数'T'

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. string已经可以为空,因为它是一个引用类型。 You don't need to wrap it in Nullable in order to have a null value. 您不需要将其包装在Nullable中以获得空值。 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 . 只有非可空值的类型可以用作Nullable的泛型参数。

String Class is a class, it not a struct like System.Int32 or other primitive types. 串类是一类,它不是一个structSystem.Int32或其它原始类型。 It can hold null value. 它可以保存空值。 Nullable<T> works with value types. Nullable<T>与值类型一起使用。

From the name it appears that you want to store DateTime object. 从名称看,您想要存储DateTime对象。 Its always better to have DateTime in its own type ie. DateTime放在自己的类型中总是更好。 DateTime , for Nullable you can use Nullable<DateTime> or DateTime? DateTime ,对于Nullable,你可以使用Nullable<DateTime>DateTime?

If you look at the docs on MSDN about Nullable<T> you will notice that T is constrained with struct. 如果您查看MSDN上有关Nullable <T>的文档,您会注意到T受结构约束。 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!). 对Type参数的约束表明,这样的约束将泛型参数限制为除Nullable <T>之外的值类型(请注意Nullable <T>是结构!)。

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. 正如MSDN文档所说,string是一种引用类型,这意味着为Nullable <T>创建的泛型参数约束将使Nullable <string>等类型无效,或者将任何引用类型作为泛型参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“ T1”必须是不可为空的值类型,以便在通用类型或方法“ System.Nullable”中将其用作参数“ T” <T> &#39; - The type 'T1' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>' 类型&#39;T&#39;必须是非可空值类型,以便在泛型类型或方法&#39;System.Nullable中将其用作参数&#39;T&#39; <T> “ - The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>' 类型“字符串”必须是不可为空的类型,以便将其用作泛型类型或方法“System.Nullable”中的参数 T<T> &#39; - The type 'string' must be a non-nullable type in order to use it as parameter T in the generic type or method 'System.Nullable<T>' 该类型必须是不可为空的值类型,以便在通用类型或方法“ System.Nullable”中将其用作参数“ T” <T> “ - The type must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>' 为了在通用类型或方法中将其用作参数“ T”,类型“字符串”必须为非空值类型 - The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 类型&#39;MyObject&#39;必须是非可空值类型才能在泛型类型或方法&#39;Nullable中将其用作参数&#39;T&#39; <T> “ - The type 'MyObject' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>' 该类型(我的类)必须为非空类型,以便在通用方法中用作参数“ T” - the type (my class) must be non-nullable type in order to use as a parameter 'T' in a generic method 类型必须是不可为空的值 - The type must be a non-nullable value 必须是不可为空的才能用作参数'T' - must be non-nullable in order to use as parameter 'T' 使用 Struct 作为通用参数并转换到基接口会导致错误:必须是不可为空的值类型 - Using Struct as Generic Parameter and casting to the base interface cause the error: must be a non-nullable value type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM