为什么Nullable< T>考虑一个结构,而不是一个类? [英] Why is Nullable<T> considered a struct and not a class?
问题描述
我试图定义一个泛型类,它接受任何可以设置为null的类型:
public abstract class BundledValue_Classes< T>
其中T:class
{
private Tuple< T,object> _bundle = new Tuple< T,object>();
public T Value
{
get {return _bundle == null? null:_bundle.Item1; }
set {_bundle = new Tuple< T,object>(value,obj); }
}
// ...
这很好。我也想创建一个可以接受任何基本类型(int,double等)的派生类,并简单地将其解析为与上述类相同的类,但是其中的基元被包装在Nullable $ b $中b
public abstract class BundledValue_Structs< T>
:BundledValue_Classes< T?>
其中T:struct
{
// ...
}
但由于某种原因,这不起作用。我得到的错误是:类型'T?'必须是引用类型才能在通用类型或方法'BundledValue_Classes< T>'中使用它作为参数'T'
是否清楚我在这里要做什么?我不想复制粘贴整个 BundledValue_Classes< T>
类的内容来处理包含在 Nullable< T>
。
也许有一些方法可以在 BundledValue_Classes< T>
,这样它不仅可以接受类,而且还可以为任何类型分配一个空值(包括 Nullable< T>
类型,这似乎是唯一的)
可为空< T>
是一个结构体,根据文档。它根本不是一个班级。它是一个值类型,在字段方面看起来有点像这样:
public struct Nullable< T>
{
私人只读T值;
private readonly bool hasValue;
null
值对于 Nullable< T>
类型不是空引用,它只是 hasValue
字段为false的值。
请注意, Nullable< T>
不满足约束条件其中TFoo:struct
然而,因为它实际上是一个不可空值值类型约束。基本上 Nullable< T>
不满足类
或 struct
约束。 不存在的限制,只允许可为空的类型(引用或可为空< T>
)。
I'm trying to define a generic class that takes any type that can be set to null:
public abstract class BundledValue_Classes<T>
where T : class
{
private Tuple<T, object> _bundle= new Tuple<T, object>();
public T Value
{
get { return _bundle == null ? null : _bundle.Item1; }
set { _bundle = new Tuple<T, object>(value, obj); }
}
//...
This works fine. I also want to create a derived class that can take any primitive type (int, double, etc), and simply resolve that into the same as the above class, but where the primitive is wrapped in a Nullable
public abstract class BundledValue_Structs<T>
: BundledValue_Classes<T?>
where T : struct
{
//...
}
But for some reason, this doesn't work. I get the error: The type 'T?' must be a reference type in order to use it as parameter 'T' in the generic type or method 'BundledValue_Classes<T>'
Is it clear what I'm trying to do here? I don't want to have to copy paste the contents of the whole BundledValue_Classes<T>
class just to handle the case of a primitive wrapped in a Nullable<T>
.
Perhaps there's some way to set the type constraint on BundledValue_Classes<T>
so that it will not only accept classes, but any type that can be assigned a null value (including Nullable<T>
types, which appear to be the only exception to the rule.)
Nullable<T>
is a struct, as per the documentation. It simply isn't a class. It's a value type, which looks a bit like this in terms of fields:
public struct Nullable<T>
{
private readonly T value;
private readonly bool hasValue;
}
The null
value for a Nullable<T>
type isn't a null reference, it's just a value where the hasValue
field is false.
Note that Nullable<T>
doesn't satisfy a condition for a constraint of where TFoo : struct
however, as that's actually a non-nullable value type constraint. Basically Nullable<T>
doesn't satisfy either the class
or the struct
constraint. There's no constraint which does only allow for nullable types (reference or Nullable<T>
).
这篇关于为什么Nullable< T>考虑一个结构,而不是一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!