码迷,mamicode.com
首页 > Windows程序 > 详细

C#基础_单例模式

时间:2016-06-15 15:56:15      阅读:253      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!