■ 제네릭 싱글톤 (Generic Singleton)

○ 특징과 용도

○ 제네릭 싱글톤 구현 예시

public class Singleton<T> where T : class, new()
{
    private static readonly Lazy<T> _instance = new Lazy<T>(() => new T());

    public static T Instance => _instance.Value;

    // 생성자를 private으로 설정하여 외부에서 인스턴스 생성을 방지
    private Singleton() { }
}

○ 사용 예시

public class MyClass { }

var instance1 = Singleton<MyClass>.Instance;
var instance2 = Singleton<MyClass>.Instance;

// instance1과 instance2는 동일한 참조를 가짐
Console.WriteLine(object.ReferenceEquals(instance1, instance2)); // true

■ 연습 : 싱글톤 베이스와 매니저 스크립트