Generic argument as a base class

H

Harold Howe

Howdy all.

Is it possible to use a generic argument as a base class? Based on the
error that I am getting, I think the answer is no.

public class Test<T> : T
{

}

main.cs(5,24): error CS0689: Cannot derive from 'T' because it is a type
parameter


This feature would be most useful, particularly in the design of a class
library. It would allow users to inject different behaviors into the
leaf classes in the hierarchy.

H^2
 
M

Michael Bray

This feature would be most useful, particularly in the design of a class
library. It would allow users to inject different behaviors into the
leaf classes in the hierarchy.

I'm not quite sure what you are trying to get at, but it seems that this
wouldn't buy you anything... What/how would you actually use the Test<T>
class? It would be exactly the same as the T class itself. You wouldn't
be able to override any methods of T because the programmer (not to mention
the compiler) doesn't know what the methods of T are.

Maybe you are searching for 'interfaces'?

-mdb
 
H

Harold Howe

I'm not quite sure what you are trying to get at, but it seems that this
wouldn't buy you anything...

This is a common technique for implementing the strategy pattern,
especially in other languages. See Modern C++ by Alexandrescu.

Here is a contrived example that uses graphical visio like objects.

class BaseNode
{
public Rectangle Bounds;
public Point Location;
public Size Size;
public virtual void Paint(Graphics g){}
}

class SolidNode
{
public Color BackColor;
public override void Paint(Graphics g)
{
using(SolidBrush brush = new SolidBrush(BackColor)
g.FillRectangle(brush, bounds);
}
}

class GradientNode
{
public Color StartColor;
public Color EndColor;
public float Angle;

public override void Paint(Graphics g)
{
using(LinearGradientBrush brush ... )
g.FillRectangle(brush, bounds);
}
}

class EllipticalNode<TBackgroundType> : TBackgroundType
where TBackgroundType : BaseNode
{
public override void Paint(Graphics g)
{
// set eliptical clip region
// paint background
base.Paint(g);

// paint elliptical border
g.DrawElipse(...)
}
}


class BoxNode<TBackgroundType> : TBackgroundType
where TBackgroundType : BaseNode
{
public override void Paint(Graphics g)
{
// paint background
base.Paint(g);

// paint rectangular border
g.DrawRectangle(...)
}
}

The purpose of the templates is to alter the behavior of the leaf
classes via base class selection. Here, the base class dictates how the
background of the node is painted. Without generics, the two leaf
classes would balloon to four classes (GradientBox, SolidBox,
GradientElipse, SolidElipse), or the classes would need to use
aggregation (ie you would pass them an object that is responsible for
painting the background).

H^2
 
R

Randy A. Ynchausti

Harold,
Is it possible to use a generic argument as a base class? Based on the
error that I am getting, I think the answer is no.

public class Test<T> : T
{

}

main.cs(5,24): error CS0689: Cannot derive from 'T' because it is a type
parameter

The reason this is not allowed is that the type of T is not known at
compile-time. Therefore, the compiler does not know what the superclass of
Test<T> is.

I know it seems that if the compiler can figure out <T>, the compiler should
be able to figure out : T. However, these are two are different things in
reality. <T> represents the generic type that the compiler expects
developers to define when creating instances of the Test<T> class, when
extending the class, etc. However, : T defines the superclass of Test<T>.
Since T is unknown at compile time, Test<T> can not inherit from T.

I hope that helps.

Regards,

Randy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top