Nullable<T> 实现

3

我正在尝试实现Nullable类型。但是下面提到的代码不支持valuetype数据类型的空值。

using System;
using System.Runtime;
using System.Runtime.InteropServices;

namespace Nullable
{

    [Serializable, StructLayout(LayoutKind.Sequential)]    
    public struct Nullable<T> where T : struct
    {
        private bool hasValue;

        public bool HasValue
        {
            get { return hasValue; }
        }

        internal T value;

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

        public T Value
        {
            get
            {
                if (!this.hasValue)
                {
                    new InvalidOperationException("No value assigned");
                }
                return this.value;
            }
        }

        public T GetValueOrDefault()
        {
            return this.value;
        }

        public T GetValueOrDefault(T defaultValue)
        {
            if (!this.HasValue)
            {
                return defaultValue;
            }
            return this.value;

        }

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

        public override int GetHashCode()
        {
            if (!this.hasValue)
            {
                return 0;
            }
            return this.value.GetHashCode();
        }

        public override string ToString()
        {
            if (!this.hasValue)
            {
                return string.Empty;
            }
            return this.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;
        }
    }
}

当我尝试将值赋为null时,它会抛出一个错误:"Cannot convert null to 'Nullable.Nullable' because it is a non-nullable value type"

要解决这个问题,我该怎么做?


6
为什么要重复造轮子? - Selman Genç
由于多种原因,无法创建具有与.NET类型相同功能的“Nullable”类型。这只是其中之一。 - Servy
2
我只是想学习,它们是如何在框架中实现的。 - Karthick NS
我非常确定编译器和运行时有一些“特殊情况”的实现,专门用于处理系统提供的Nullable类型,而你无法自己重新实现它们。 - CodingWithSpike
1
这里有一些你可以从中做笔记的内容:nullable.cs - RadioSpace
3个回答

7
null 分配给 Nullable<T> 只是对分配 new Nullable<T>() 的语法糖而已,它是 C# 语言的一部分,你无法将此功能添加到自定义类型中。
C# 规范: 4.1.10 可空类型 可空类型可以表示其基础类型的所有值以及一个额外的 null 值。可空类型写作 T?,其中 T 是基础类型。这种语法是 System.Nullable 的简写形式,两种形式可以互换使用。 6.1.5 null 字面量转换 从 null 字面量到任何可空类型都存在隐式转换。该转换会生成给定可空类型的 null 值(§4.1.10)。

2
你不能这样做。
Nullable<T> 是一个特殊情况。它在CLR级别上有特殊的处理(这不是纯粹的C#语言特性 - 特别是,CLR支持对可空类型进行装箱/拆箱的特殊情况)。

1
你正在声明自己的Nullable为一个struct,而struct不可为空。 你应该将其声明为class。
将Point类型声明从struct改为class,此代码应该抛出与您遇到的相同错误。
void Main()
{
    Point p = null;
}

// Define other methods and classes here
struct Point
{
 public int X   {get; set;}
 public int Y   {get; set;}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接