[英]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. 串类是一类,它不是一个struct
象System.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.