How to make a generic singleton base class C#
c#, generics, singleton
Solution
The problem is your generic constraint `where T : class, new()`. The `new()` constraint requires a public, parameterless constructor on T. There is no way around this; you need to provide such a constructor in `Permission Controller`.
Problem
I am trying to create a generic singleton base class like ``` public class SingletonBase<T> where T : class, new() { private static object lockingObject = new object(); private static T singleTonObject; protected SingletonBase() { } public static T Instance { get { return InstanceCreation(); } } public static T InstanceCreation() { if(singleTonObject == null) { lock (lockingObject) { if(singleTonObject == null) { singleTonObject = new T(); } } } return singleTonObject; } } ``` But I have to make constructor as public in derived one. ``` public class Test : SingletonBase<Test> { public void A() { } private Test() : base() { } } ``` Compilation Error: 'Test' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'Test' How can I achieve this?