实现Nullable 可空类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace demo12
{
    class Program
    {
        static void Main(string[] args)
        {
            Animal<int> a = null;
        }
    }

    struct Animal<T> 
    {

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

        //重载个方法就可以=null
        public static implicit operator Animal<T>(string value)
        {
            if (value == null)
                return new Animal<T>();
            else
            {
                throw new Exception("aa");
            }
        }
    }
}
原文地址:https://www.cnblogs.com/tiancai/p/6418635.html