首页 > Windows开发 > 详细

C#基础_单例模式

时间:2016-06-15 15:54:48      阅读:257      评论:0      收藏:0      [点我收藏+]

控制某个类型的实例数量-唯一一个

class Program
    {
        static void Main(string[] args)
        {
           test t1 = test.GetInstance();
           test t2 = test.GetInstance();
           Console.WriteLine(t1.GetHashCode()==t2.GetHashCode());

           Test T1 = new Test();
           Test T2 = new Test();
           Console.WriteLine(T1.GetHashCode()==T2.GetHashCode());
           Console.ReadKey();
        }
    }
    //单例模式
    public class test
    {
        private test() { }
        public static readonly test instance = new test();
        public static test GetInstance()
        {
            return instance;
        }

    }
    //普通类 
    public class Test
    {
    }

第一个哈希值相同,表明是一个实例;

第二个不同

C#基础_单例模式

原文:http://www.cnblogs.com/shinchan/p/5587491.html

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