为什么C#空可以隐含转换为System.Nullable< T&GT ;,但不能定义自可空< T> [英] Why c# null can implicit convert to System.Nullable<T>, but can not a self defined Nullable<T>

查看:130
本文介绍了为什么C#空可以隐含转换为System.Nullable< T&GT ;,但不能定义自可空< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么可以隐式转换为 System.Nullable< T> 是这样的:

 诠释? VAL = NULL;
 

但自定义的可空< T> (从.NET参考源修改)不能分配,有一些编译器的魔力?谁能告诉我更多的内部执行力度?

  [Serializable接口]
公共结构可空< T>其中T:结构
{
    私人布尔hasVa​​lue的;
    内部的T值;

    公众可空(T值)
    {
        THIS.VALUE =价值;
        this.hasVa​​lue =真;
    }

    公共BOOL hasVa​​lue的
    {
        得到
        {
            返回hasVa​​lue的;
        }
    }

    公众吨价
    {
        得到
        {
            如果(!hasVa​​lue的)
            {
                抛出新的异常();
            }
            返回值;
        }
    }

    公共牛逼GetValueOrDefault()
    {
        返回值;
    }

    公共牛逼GetValueOrDefault(T设置defaultValue)
    {
        返回hasVa​​lue的?值:设置defaultValue;
    }

    公众覆盖布尔等于(对象等)
    {
        如果(!hasVa​​lue的)返回其他== NULL;
        如果(其他== NULL)返回false;
        返回value.Equals(其它);
    }

    公众覆盖INT GetHash code()
    {
        返回hasVa​​lue的? value.GetHash code():0;
    }

    公共重写字符串的ToString()
    {
        返回hasVa​​lue的? value.ToString():;
    }

    公共静态隐含的运营商可空< T>(吨价)
    {
        回到新的可空< T>(值);
    }

    公共静态明确运营商T(可空< T>值)
    {
        返回value.Value;
    }
}
 

下面的测试code,编译错误

 可空< INT> X =零; //错误无法将null转换为可空< INT>因为它是一个非空的值类型
 

解决方案

科C#5.0规范的6.1.5:

  

6.1.5 null文本转换
  隐式转换存在从null文本到任何可空类型。这种转换产生给定可空类型的空值(§4.1.10)。

请注意,这个编译器提供的隐式转换只存在于可空类型的。您自定义的可空< T> 不是由C#规范中定义的可空类型。这只是一些结构,你已经宣布,有很多内置的可空1所述的功能; T> 键入(中所引用的部分4.1.10所述),但事实上并非可为空的每定义在C#

Why null can implicit convert to System.Nullable<T> like this:

int? val = null;

but self defined Nullable<T> (modified from .net reference source) cannot assign null, is there some compiler magic? Could anyone tell me more internal implimentation?

[Serializable]
public struct Nullable<T> where T : struct
{
    private bool hasValue;
    internal T value;

    public Nullable(T value)
    {
        this.value = value;
        this.hasValue = true;
    }

    public bool HasValue
    {
        get
        {
            return hasValue;
        }
    }

    public T Value
    {
        get
        {
            if (!HasValue)
            {
                throw new Exception();
            }
            return value;
        }
    }

    public T GetValueOrDefault()
    {
        return value;
    }

    public T GetValueOrDefault(T defaultValue)
    {
        return HasValue ? value : defaultValue;
    }

    public override bool Equals(object other)
    {
        if (!HasValue) return other == null;
        if (other == null) return false;
        return value.Equals(other);
    }

    public override int GetHashCode()
    {
        return HasValue ? value.GetHashCode() : 0;
    }

    public override string ToString()
    {
        return HasValue ? value.ToString() : "";
    }

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

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

test code below, compile error

Nullable<int> x = null; //ERROR Cannot convert null to 'Nullable<int>' because it is a non-nullable value type

解决方案

Section 6.1.5 of the C# 5.0 specification:

6.1.5 Null literal conversions
An implicit conversion exists from the null literal to any nullable type. This conversion produces the null value (§4.1.10) of the given nullable type.

Note that this compiler-provided implicit conversion exists only to nullable types. Your custom-defined Nullable<T> is not a nullable type as defined by the C# specification. It's just some struct that you've declared that has a lot of the features of the built-in Nullable<T> type (described in the referenced section 4.1.10), but which is not in fact "nullable" per the definition in C#.

这篇关于为什么C#空可以隐含转换为System.Nullable&LT; T&GT ;,但不能定义自可空&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆