用C#实现一个类,要求该类只能被实例化一次
1Singleton模式:
2using System;
3public class Singleton
4{
5private static Singleton instance;
6private Singleton() {}
7public static Singleton Instance
8{
9get
10{
11if (instance == null)
12{
13instance = new Singleton();
14}
15return instance;
16}
17}
18}
19
2using System;
3public class Singleton
4{
5private static Singleton instance;
6private Singleton() {}
7public static Singleton Instance
8{
9get
10{
11if (instance == null)
12{
13instance = new Singleton();
14}
15return instance;
16}
17}
18}
19
using System;
public class Singleton
{
private static readonly Singleton instance=new Singleton() ;
private Singleton() {}
public static Singleton Instance
{
return instance;
}
}
白虎
Hello! I Am David.DU.