Question: Disclaimer: Theoretical Question
================================

> 
> The new constraint specifies that any
>  type argument in a generic class
>  declaration must have a public
>  parameterless constructor.
> 
> 
> Source: <http://msdn.microsoft.com/en-us/library/sd2w2ew5(v=vs.80).aspx>
> 
> 
> 

What if I wanted my generic class to have a **protected** parameterless constructor instead? For instance, if I want to write a `Singleton` class which I "attach" to other classes to make them `Singleton`s, I don't want the derived classes to be instantiable - everything should go through the `.Instance` property.

```
internal class Singleton<T> where T : new()
{
    public static T Instance { get; private set; }

    static Singleton()
    {
        Singleton<T>.Instance = new T();
    }
}

internal class OnlyOneOfMe : Singleton<OnlyOneOfMe>
{
    protected OnlyOneOfMe()
    {
    }
}

```

This way, `Singleton<T>` is able to create the only instance of the `OnlyOneOfMe` class, but nothing else can (unless it is a subclass).

"What if a generic parent class could access the generic type's protected members?"

Answer: 
You can call a protected constructor using [reflection](http://www.codingday.com/reflection-with-private-members/). However this should raise warning signs that you are doing something you are not supposed to. In most cases, you should be able to avoid a singleton and use [dependency injection](http://msdn.microsoft.com/en-us/magazine/cc163739.aspx) instead. If that doesn't work either, you can use something like the ambient context pattern ([see my answer here](https://stackoverflow.com/questions/5776261/any-good-way-for-getting-a-single-instance-instead-of-using-singleton-in-c/5776599#5776599)).
.NET would not know that you don't want to accept 

```
class OnlyOneOfMe : Singleton<Other>

```

as a valid class. Since it is actually valid it will try to make the class and needs a public Other constructor.