using System;
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
A a = Singleton<A>.GetInstance();
B b = Singleton<B>.GetInstance();
B b1 = Singleton<B>.GetInstance();;
if (a.Equals(b))
Console.WriteLine("a Equals b: true");
else
Console.WriteLine("a Equals b: false");
if (b.Equals(b1))
Console.WriteLine("b Equals b1: true");
else
Console.WriteLine("b Equals b1: false");
RL();
}
#region Helper methods
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
public class A
{
public A()
{}
}
public class B
{
public B()
{}
}
/// <summary>
/// Singleton泛型类
/// </summary>
public sealed class Singleton<T> where T : new()
{
private static readonly T instance = new T();
private Singleton() { }
public static T GetInstance()
{
return instance;
}
}
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
A a = Singleton<A>.GetInstance();
B b = Singleton<B>.GetInstance();
B b1 = Singleton<B>.GetInstance();;
if (a.Equals(b))
Console.WriteLine("a Equals b: true");
else
Console.WriteLine("a Equals b: false");
if (b.Equals(b1))
Console.WriteLine("b Equals b1: true");
else
Console.WriteLine("b Equals b1: false");
RL();
}
#region Helper methods
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
public class A
{
public A()
{}
}
public class B
{
public B()
{}
}
/// <summary>
/// Singleton泛型类
/// </summary>
public sealed class Singleton<T> where T : new()
{
private static readonly T instance = new T();
private Singleton() { }
public static T GetInstance()
{
return instance;
}
}