C# Generics not constraining correctly?

Hi,

I have a problem, I want to use a generic node interface like this:

interface Node<T>
{
     int doStuff(T other);
}

I want to make sure that an implementation would always have to be like this:

class Waypoint : Node<Waypoint>

Therefore I declared my node interface like this:

interface Node<T> where T : Node<T>

However, C# seems to ignore it when I use it like this:

class Waypoint : Node<Waypoint>

class WrongClass : Node<Waypoint>

This should not work, since Waypoint is not of type WrongClass? The compiler does not mark it for an error and it does not produce any kind of runtime error when I use

int doStuff(Waypoint other) 

in a WrongClass object.

Is there any way to acheive the desired restriction?

greetings

FreetimeCoder

What you have should indeed compile fine. Waypoint implements Node of Waypoint, and is therefore suitable for use wherever Node of T is required. C# doesn’t provide a mechanism to enforce that you use a certain type for generic parameters. You’ve done the best you can.