Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6079451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:56:53+00:00 2026-05-23T10:56:53+00:00

I have an interface, which in this example we’ll call IMagicPerson public interface class

  • 0

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:

  • A MagicPowerCollection class that can enumerate through multiple MagicPower enumerators as if it were one enumerator (using yield return statements) and using this to add a private static MagicPower[] to base.Powers
  • Using another constructor of the MagicPowerCollection that can add single Powers to the end of the enumerator (again, using yield returns)

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

  • Overloading the + operator of IEnumerable to allow adding more enumerators or single Powers

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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:56:53+00:00Added an answer on May 23, 2026 at 10:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code (which is way simplified from the real code): public interface
I have an interface called Dictionary which has a method insert() . This interface
I have a generic interface IConstrained which is implemented by the generic Constrained class.
I have an interface which is generic: public interface DataTable < T > {
I have the following interfaces defined: public interface IAudit { DateTime DateCreated { get;
Lifting this example from Wikipedia : // the Window interface interface Window { public
We have an interface which will be implemented by classes that take care of
I have the following interface which I'm trying to make COM-visible. When I try
In COM, if I have an interface IBase and an interface IX which inherits
We have an interface IPoller for which we have various implementations. We have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.