单例
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);//实现 } } }
时间: 2024-10-11 10:36:57