Home > Net >  Is there a difference between "SomeClass<T> where T : SomeOtherClass" and "Some
Is there a difference between "SomeClass<T> where T : SomeOtherClass" and "Some

Time:08-02

I am learning C# and .net and came across this usage of "where":

public class DbContextOptions<TContext> : DbContextOptions where TContext : DbContext

Why not simply say

public class DbContextOptions<DbContext> : DbContextOptions

Are they not equivalent? Or I am totally missing some aspect of this?

regards,

CodePudding user response:

No, they are doing different things. The second one isn't doing what you think it is and forcing the generic type to be a DbContext. Instead, it's just naming the generic type inside that class as DbContext. Whether you call it TContext, DbContext or Bob it doesn't make any difference as the name is only used inside the class. But using DbContext will clash with the "real" DbContext type. So you would still need the generic type constraint.

  • Related