Search code examples
c#genericsabstract-classabstract

Does it matter if a class is abstract when it uses Generics?


I.e, does the abstract modifier do anything here:

public abstract class MyClass<T> where T : class {
}

You can't use Generic classes on their own anyway, so what difference does it make if it's abstract or not?


Solution

  • With the abstract keyword, you have to subclass it to use it.

    class MySubclass : MyClass<string>
    {
    }
    
    var o = new MySubclass();
    

    Without it, you can instantiate directly:

    var o = new MyClass<string>();