相关疑难解决方法(0)

为什么struct不能有无参数构造函数

为什么struct不能有无参数构造函数?为CLR做这件事有什么问题?为什么不允许这样做?请解释一下,因为我不明白.

c#

13
推荐指数
1
解决办法
2万
查看次数

为什么Nullable <T>可以为空?为何无法复制?

当我写作

Nullable<Nullable<DateTime>> test = null;

我收到编译错误:

The type 'System.Datetime?' must be a non-nullable value type in order to use it as a paramreter 'T' in the generic type or method 'System.Nullable<T>'

Nullable<T>它是struct如此,它应该是不可空的.

所以我试着创建这个struct:

public struct Foo<T> where T : struct
{
    private T value;

    public Foo(T value)
    {
        this.value = value;
    }

    public static explicit operator Foo<T>(T? value)
    {
        return new Foo<T>(value.Value);
    }

    public static implicit operator T?(Foo<T> value)
    {
        return new Nullable<T>(value.value);
    } …
Run Code Online (Sandbox Code Playgroud)

c# nullable

5
推荐指数
1
解决办法
1699
查看次数

标签 统计

c# ×2

nullable ×1