4

Possible Duplicates:
Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates?
In C# 4.0, is it possible to derive a class from a generic type parameter?

I'm trying to create a generic class that inherits from its generic type (in 1.Attempt). Looks like it's impossible. But in other attempt i must cast the object. Any design solution to that problem?

// #1 Attempt
public interface IFoo { }

public class Generic<T> : T { }

public void play ()
{
    IFoo genfoo = new Generic<IFoo> ();
}

.

// #2 Attempt. Castable solution
public interface IAnything { }

public interface IFoo2 : IAnything { }

public class Generic2<T> : IAnything { }

public void play2 ()
{
    IFoo2 genFoo = (IFoo2) new Generic2<IFoo2> ();
}
Community
  • 1
  • 1
jack-london
  • 1,599
  • 3
  • 21
  • 42
  • I can't see that this is possible, with or without casting. What are you trying to do? Based on your scenario it may be that you're looking for something like Castle.DynamicProxy, or perhaps a mocking framework... – MattDavey May 05 '11 at 09:27
  • What is the point in having a generic class that has to inherit from the generic type as well? It **defeats** the generic objective. – Aliostad May 05 '11 at 09:28
  • What do you mean,*inheriting* or *implementing*? You say "inherit" but give an interface as an example. – Cheng Chen May 05 '11 at 09:32
  • Also, plenty of good info here: http://stackoverflow.com/questions/1849107/what-are-the-good-reasons-to-wish-that-net-generics-could-inherit-one-of-the-gen – spender May 05 '11 at 09:47
  • Also, this was asked yesterday as well: http://stackoverflow.com/questions/5890516/in-c-4-0-is-it-possible-to-derive-a-class-from-a-generic-type-parameter/5890813#5890813 – Eric Lippert May 05 '11 at 15:38

3 Answers3

1

I don't see the point of the code you provided in Attempt #1. Why would you inherit a class and then pass that same class as generic param.

Nevertheless, generic inheritance is supported but its done differently, please check this link, it might give you an idea: Generic Inheritance

Hope this helps.

Elio
  • 463
  • 6
  • 18
0

What you are doing defeats the generic objective.

How can I just use something like this:

Generic<int> intgen = new Generic<int>(); // Impossible

The problem is generic defines a behaviour which is type-unspecific. your generic is type specific since it has to inherit from it. Generic is used to encapsulate a functionality that cannot be expressed in inheritance while you are mixing the two - and I can tell you it ain't gonna be a good mix.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 1
    `List list = new List(); //????` – jungle_mole Apr 19 '15 at 17:02
  • 1
    actually, no, it is type unspecific, until you instantiate it, so really inheriting from a generic type should be allowed, it's a lame constraint –  Feb 27 '16 at 02:45
0

You can't do this because T in the definition of the generic is a type definition and not a real class. I'm not sure what you are trying to accomplish, though, because in my opinion your problem could be solved with a simple inheritance. If you are trying to add some behavior to your class then you can simply use a generic and derive from that:

public class Behavior<T>
{
  ...
}

public class DerivedBehavior<T> : Behavior<T>
{
  ...
}
LazyOfT
  • 1,428
  • 7
  • 20