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;
    }
}

相关文章:

  • 2022-02-10
  • 2021-06-29
  • 2022-01-30
  • 2021-11-30
  • 2021-05-27
  • 2021-07-28
  • 2021-11-06
  • 2022-12-23
猜你喜欢
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
  • 2021-05-21
  • 2022-12-23
相关资源
相似解决方案