简体   繁体   English

类型“ 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>'

Here iam getting error 我在这里得到错误

The type 'string' must be a non-nullable value type in order to use it as parameter 'T1' in the generic type or method 'GetDataKeyValue(System.Web.UI.WebControls.GridView, int, string)' 为了在通用类型或方法“ GetDataKeyValue(System.Web.UI.WebControls.GridView,int,string)”中将其用作参数“ T1”,类型“字符串”必须为非空值类型

String Process = GetDataKeyValue<String>(gvTargetRate, RowIndex, "Process");

Here iam getting error 我在这里得到错误

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' 为了在通用类型或方法“ System.Nullable”中将其用作参数“ T”,类型“ T1”必须为非空值类型

private Nullable<T> GetDataKeyValue<T>(GridView gv, int RowIndex, T column)
{
    if (column == null)
        return default(T);

    return (T)gv.DataKeys[RowIndex].Values[column];
}

You can't create a Nullable<> out of any type. 您不能创建任何类型的Nullable<> As the compiler tells you, you can only do that for value types. 正如编译器告诉您的那样,您只能对值类型执行此操作。 Since the method isn't restricted to any type, it can't say for sure you won't pass it, let's say, an object. 由于该方法不受任何类型的限制,因此不能肯定地说您不会传递一个对象。

What you want isn't possible, actually, as explained in this question and its answer . 实际上,正如该问题及其答案中所解释的,您想要的是不可能的。

And by the way, T isn't a nullable type by definition, so default(T) will become a non-null value in the case of value types. 顺便说一句,从定义上说T不是可为空的类型,因此对于值类型, default(T)将成为非空值。 string is on object, so it can be null without the use of Nullable<T> . string在对象上,因此如果不使用Nullable<T> ,则它可以为null。

The Nullable type requires that T is a non-nullable value type, for example int or DateTime. 可空类型要求T是不可空值类型,例如int或DateTime。 Reference types like string can already be null. 像字符串这样的引用类型可以已经为空。 There would be no point in allowing things like Nullable so it is disallowed. 允许Nullable之类的东西是没有意义的,因此是不允许的。

more info. 更多信息。

暂无
暂无

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

相关问题 类型&#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>' 类型'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>' 该类型必须是不可为空的值类型,以便在通用类型或方法“ 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>' 类型“字符串”必须是不可为空的类型,以便将其用作泛型类型或方法“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>' 为了在通用类型或方法中将其用作参数“ 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 必须是不可为空的才能用作参数'T' - must be non-nullable in order to use as parameter 'T' 帮助C#generics错误 - “类型'T'必须是不可为空的值类型” - Help with C# generics error - “The type 'T' must be a non-nullable value type” 类型必须是不可为空的值 - The type must be a non-nullable value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM