从Nullable隐式转换为正常,任何想法?

Mus*_*gdy 6 c# nullable implicit-conversion

我想知道如何隐式转换为可空的"?" 区变量的变量.

给出这个例子

int? x = 5;

int y = x; //this gonna fail, !!!
Run Code Online (Sandbox Code Playgroud)

我需要一些方法来覆盖=参数,但遗憾的是=参数不能超载...任何建议

我正在使用C#

Joh*_*sch 13

您有两个选项,直接访问该值(如果您确定它不是null):

int y = x.Value;
Run Code Online (Sandbox Code Playgroud)

或者,使用null合并运算符:

int y = x ?? 0; // 0 if null...
Run Code Online (Sandbox Code Playgroud)


Sco*_*ner 7

可以实现隐式转换运算符,但只能实现您定义的类型.例如,做这样的事情..

public class NullableExtensions 
{
    public static implicit operator int(int? value)
    {
        return value ?? default(int);
    }
}
Run Code Online (Sandbox Code Playgroud)

..将返回CS0556编译错误,因为强制转换不包含用户定义的类型.

你可以做的最接近的是定义你自己的Nullable类型,它包含一个隐式的强制转换操作符:

public struct ImplicitNullable<T> where T: struct
{
    public bool HasValue { get { return this._value.HasValue; } }
    public T Value { get { return this._value.Value; } }

    public ImplicitNullable(T value) : this() { this._value = value; }
    public ImplicitNullable(Nullable<T> value) : this() { this._value = value; }

    public static implicit operator ImplicitNullable<T>(T value) { return new ImplicitNullable<T>(value); }
    public static implicit operator ImplicitNullable<T>(Nullable<T> value) { return new ImplicitNullable<T>(value); }

    public static implicit operator T(ImplicitNullable<T> value) { return value._value ?? default(T); }
    public static implicit operator Nullable<T>(ImplicitNullable<T> value) { return value._value; }

    private Nullable<T> _value { get; set; }

    // Should define other Nullable<T> members, especially 
    // Equals and GetHashCode to avoid boxing
}
Run Code Online (Sandbox Code Playgroud)

请注意,尽管可以编写此代码,但可能会导致难以跟踪错误.我建议使用显式强制转换,或者在值为时抛出异常null.

之后,您可以按预期进行投射:

static void Main()
{
    int myInt = 1;
    int? nullableInt = 2;

    ImplicitNullable<int> implicitInt;

    // Convert from int or int?
    implicitInt = myInt;
    implicitInt = nullableInt;

    // Convert to int or int?
    myInt = implicitInt;
    nullableInt = implicitInt;
}
Run Code Online (Sandbox Code Playgroud)