首页 > 其他 > 详细

实现Nullable 可空类型

时间:2017-02-20 13:43:49      阅读:239      评论:0      收藏:0      [点我收藏+]
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");
            }
        }
    }
}

 

实现Nullable 可空类型

原文:http://www.cnblogs.com/tiancai/p/6418635.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!