RCIX
RCIX

Reputation: 39437

What use is this code?

I can't figure out the use for this code. Of what use is this pattern?

[code repeated here for posterity]

public class Turtle<T> where T : Turtle<T>
{
}

Upvotes: 11

Views: 484

Answers (2)

dahlbyk
dahlbyk

Reputation: 77540

This pattern essentially allows you to refer to a concrete subclass within the parent class. For example:

public abstract class Turtle<T> where T : Turtle<T>
{
    public abstract T Procreate();
}

public class SeaTurtle : Turtle<SeaTurtle>
{
    public override SeaTurtle Procreate()
    {
        // ...
    }
}

Versus:

public abstract class Turtle
{
    public abstract Turtle Procreate();
}

public class SnappingTurtle : Turtle
{
    public override Turtle Procreate()
    {
        // ...
    }
}

In the former, it's specified that a SeaTurtle's baby will be a SeaTurtle.

Upvotes: 9

AngryHacker
AngryHacker

Reputation: 61626

There is no use that I can see. Basically, it's the same as

public class Turtle
{
}

Upvotes: -1

Related Questions