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

C#之设计模式

时间:2017-01-06 01:11:31      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:function   read   size   delegate   ring   threading   on()   cee   static   

单例

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

namespace 单例 {
    class Program {
        static void Main(string[] args) {
            Singleton instance1 = Singleton.Instance;
            Singleton instance2 = Singleton.Instance;
            bool result = object.ReferenceEquals(instance1, instance2);
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
    public sealed class Singleton {
        private static Singleton _instance;
        private static object obj = new object();
        private Singleton() { }
        public static Singleton Instance{
            get {
                if(_instance == null) {
                    lock (obj) {
                        if(_instance == null) {
                            _instance = new Singleton();
                        }
                    }
                }
                return _instance;
            }
        }
    }

}

代理

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

namespace 代理 {
    class Program {
        delegate void MyDelegate(string str);
        static void Main(string[] args) {
            MyDelegate md = new MyDelegate(DelegateText.Function);
            md("刘冠");//调用
            Console.ReadLine();
        }
    }
    //一个类调用,另一个类实现
    public class DelegateText {
        public static void Function(string str) {
            Console.WriteLine(str);//实现
        }
    }
}

 

C#之设计模式

标签:function   read   size   delegate   ring   threading   on()   cee   static   

原文地址:http://www.cnblogs.com/liuguan/p/6254618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!