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

  • Home
  • SEARCH
  • 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 6546161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:40:26+00:00 2026-05-25T11:40:26+00:00

I am looking to build a java-style enum pattern for C# that also supports

  • 0

I am looking to build a java-style enum pattern for C# that also supports inheritance. I’m having trouble with yield return. Specifically, returning the BaseEnum’s Values from the ChildEnum’s Values property.

public class BaseEnum {
    public static readonly BaseEnum A = new BaseEnum("A");
    public static readonly BaseEnum B = new BaseEnum("B");
    public static readonly BaseEnum C = new BaseEnum("C");

    public static IEnumerable<BaseEnum> Values {
        get {
            yield return A;
            yield return B;
            yield return C;
        }
    }

    public readonly String Name;

    protected BaseEnum(String name) {
        this.Name = name;
    }

    public static void TestMain() {
        Console.WriteLine("BaseEnum, should print (A,B,C):");
        foreach(BaseEnum e in BaseEnum.Values) {
            Console.WriteLine(e.Name);
        }
        Console.WriteLine("BaseEnum in ChildEnum, should print (A,B,C,D,E):");
        foreach(BaseEnum e in ChildEnum.Values) {
            Console.WriteLine(e.Name);
        }
        Console.WriteLine("ChildEnum in ChildEnum, should print (D,E):");
        foreach(ChildEnum e in ChildEnum.Values) {
            Console.WriteLine(e.Name);
        }
    }
}

class ChildEnum : BaseEnum {
    public static readonly ChildEnum D = new ChildEnum("D");
    public static readonly ChildEnum E = new ChildEnum("E");

    new public static IEnumerable<BaseEnum> Values {
        get {
            // yield return BaseEnum.Values; // This is what I want to do. It gives the error message below:
            // Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Abra.Workshop.EnumABC>' 
            // to 'Abra.Workshop.EnumABC'. An explicit conversion exists (are you missing a cast?)

            yield return D;
            yield return E;
        }
    }

    public ChildEnum(string name)
        : base(name) {
    }
}

/* Output!
BaseEnum, should print (A,B,C):
A
B
C
BaseEnum in ChildEnum, should print (A,B,C,D,E):
D
E
ChildEnum in ChildEnum, should print (D,E):
D
E
*/
  • 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-25T11:40:27+00:00Added an answer on May 25, 2026 at 11:40 am

    Use

    foreach (var b in BaseEnum.Values)
      yield return b;
    

    You need to unwrap BaseEnum.Values into individual elements.

    Or replace it with a LINQ expression:-

    return BaseEnum.Values.Concat(new[]{ D, E });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to build a query that will use the non-clustered indexing plan on
Are there any continuous integration frameworks for CL? Specifically, I'm looking for: Automated build,
I'm looking for a standalone Java library which allows me to parse LDAP style
I'm always looking for a modern Java library that makes creating valid (X)HTML snippets
Im looking to build a thread manager for an application. I have already started
We looking to build an application to maintain meeting minutes and similar stuff using
I'm looking to build an reusable control or custom helper for my MVC project.
I'm looking to build a VM into a game and was wondering if anyone
I'm looking to build a server with lots of tiny files delivered by an
I'm looking to build a quick link directory access widget. e.g. (option 1) 0-9

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.