M4rk
M4rk

Reputation: 2272

c# a Generic class of another Generic

I have a class that implement singleton, one that implement some common functions for my DB tables, and the last one that implement specific functions for Tables.

so:

 abstract class Singleton<C> where C : class, new()
 class AbstractTable<T> : Singleton<T>
 class myTable: Abstract<myTable>

The problem is that I can't pass T in second line to Singleton.

The error given is CS0452

How can I code a generic that inherits another generic?

Upvotes: 2

Views: 97

Answers (1)

Sergey Litvinov
Sergey Litvinov

Reputation: 7478

The problem with generic constraints. If you have constraints on Singleton case, and you have Generic inheritor of that class. Then this inheritor also should have the same constraints.

Like this:

abstract class Singleton<C> where C : class, new(){}
class AbstractTable<T> : Singleton<T> where T : class, new() { }
class myTable: AbstractTable<myTable>{}

Upvotes: 5

Related Questions