In C# 4.0, is it possible to derive a class from a generic type parameter?

后端 未结 4 2131
一向
一向 2020-12-06 04:18

I\'ve been trying this, but I can\'t seem to figure this out. I want to do this...

public abstract class SingletonType : TBase         


        
相关标签:
4条回答
  • 2020-12-06 04:51

    No, it's not possible, because let's say you have this class:

    class bar {
      int example = 0;
    }
    

    Now here's our type parameter class:

    class foo<T> : T {
      int example = 5;
    }
    

    If we created a variable of type foo<bar> then example would get mixed up.

    0 讨论(0)
  • 2020-12-06 04:52

    Generic types in C# are not C++ templates; remember, a generic type must work for all possible type arguments. A template need only work for the constructions you actually make.

    This question is a duplicate; see my answer to

    Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates?

    for more thoughts on this. Basically, the short answer is that the considerable costs do not outweigh the small benefits of the feature. If you don't like that answer, see my second answer:

    Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates?

    And if you don't like that answer either, see the follow-up question:

    What are the good reasons to wish that .NET generics could inherit one of the generic parameter types?

    0 讨论(0)
  • 2020-12-06 04:53

    No, this is not possible. For example, take a type that is declared sealed. You can't inherit from that class, and there is no constraint to limit to non sealed types, ergo trying to inherit from it via a generic parameter is impossible.

    0 讨论(0)
  • 2020-12-06 05:10

    Every type has exactly 1 real, discrete parent class, even when generics are involved. Even when dealing with an open generic type (e.g., typeof(List<>)), you can still find the parent class.

    If what you wanted were allowed, this would not be true, typeof(SingletonType<,> would not have a parent type, and this is not allowed.

    0 讨论(0)
提交回复
热议问题