Connell
Connell

Reputation: 14411

Overloading + operator for enumerators

I have an interface, which in this example we'll call IMagicPerson

public interface class IMagicPerson : IPerson
{
    IEnumerable<MagicPower> Powers { get; }
}

A few things inherit IMagicPerson, and then some other classes derive from them too, and so on, causing some pretty long derivative hierarchy. Each of these needs to be able to declare some of these powers, but must also use powers from the derived class.

I've played around with various ways of doing this. I've used:

But the way I'd really prefer to go about this would be by

Is this a bad way to do it? I think it looks quite nice. For example, I could have:

public class Wizard : IMagicPerson
{
    public override IEnumerable<MagicPower> Powers
    {
        get
        {
            AllPowers.AntiGravity + AllPowers.BroomStick;
            //note: AllPowers, in this example, is a static class and all the properties return a MagicPower or type derived from MagicPower
        }
    }
}

public class EvilWizard : Wizard
{
    public override IEnumerable<MagicPower> Powers
    {
        get
        {
            return base.Powers + AllPowers.InstantDeath + AllPowers.MindControl;
        }
    }
}

So, where and how would I overload the + operator in such a way? I need to be able to add a MagicPower to either an enumerator, or to another MagicPower. Both of which will return an IEnumerator<MagicPower>.

I'll also accept other suggestions on other ways of achieving this, which don't including overloading this operator.

Notes:

Bare in mind I'm using C# 2.0, so extension methods won't work (although I'm strongly considering upgrading this server just for this).

Another complication is that although the powers are usually the same for each type of MagicPerson, there are also some powers that can only used in certain situations (for example, some Wizards can't use powers in the Muggle world :D). I'm not asking how to do this, I can just use if statements or add these properties somewhere else. Just letting people know that a completely static solution won't work.

Another thing to bare in mind is that I do not have access to the IMagicPerson interface, so I can't even get the property to return a new class that inherits IEnumerable and put the overloaded operators in there (or can I?)

Thanks!!

Upvotes: 0

Views: 264

Answers (2)

Connell
Connell

Reputation: 14411

To suggest an answer to my own question:

I could create a base class for each magic person:

public abstract class MagicPerson : IMagicPerson
{
    protected List<MagicPower> powers;

    public IEnumerable<MagicPower> Powers { get { return powers; } }
}

Then add to this powers list in the constructors of the derived classes.

Upvotes: 0

SLaks
SLaks

Reputation: 887777

It is completely impossible to override an operator on an interface, in any version.

You should make a static method that takes a params IEnumerable<T>[] and yield returns all of them using a double-foreach.

Upvotes: 1

Related Questions