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 4029390
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:20:22+00:00 2026-05-20T11:20:22+00:00

I understood from this answer that C# static field initializers are executed… prior to

  • 0

I understood from this answer that C# static field initializers “are executed… prior to the first use of a static field of that class,” but that still produces results I didn’t expect, at least with generic types.

Coming from the Java world, I was missing my rich enums, and I thought with C#’s more serious generics that I ought to be able to replicate them with a minimum of boilerplate. Here (stripped of some details, like comparability) is what I came up with:

public class AbstractEnum<T> where T : AbstractEnum<T>
{
    static readonly IDictionary<String, T> nameRegistry = new Dictionary<String, T>();

    readonly String name;

    protected AbstractEnum (String name)
    {
        this.name = name;
        nameRegistry[name] = (T) this;
    }

    public String Name {
        get {
            return name;
        }
    }

    public static T ValueOf(String name) {
        return nameRegistry[name];
    }

    public static IEnumerable<T> Values {
        get {
            return nameRegistry.Values;
        }
    }
}

And some example subclasses:

public class SomeEnum : AbstractEnum<SomeEnum> {

    public static readonly SomeEnum V1 = new SomeEnum("V1");
    public static readonly SomeEnum V2 = new SomeEnum("V2");

    SomeEnum(String name) : base(name) {

    }
}

public class OtherEnum : AbstractEnum<OtherEnum> {

    public static readonly OtherEnum V1 = new OtherEnum("V1");
    public static readonly OtherEnum V2 = new OtherEnum("V2");

    OtherEnum(String name) : base(name) {

    }
}

This looks good and more or less does the trick… except that, following the letter of the spec, the actual instances (SomeEnum.V1, OtherEnum.V1 etc.) don’t get initialized unless at least one of them is referred to explicitly. Static fields/methods in the base class don’t count. So, for instance, the following:

Console.WriteLine("Count: {0}", SomeEnum.Values.Count());
foreach (SomeEnum e in SomeEnum.Values) {
    Console.WriteLine(e.Name);
}

writes Count: 0, but if I add the following line —

Console.WriteLine("SomeEnum.V1: " + SomeEnum.V1.Name);

— even after the above, I get:

Count: 2
V1
V2

(Note, by the way, that initializing the instances in a static constructor makes no difference.)

Now, I can fix this by marking nameRegistry as protected and pushing Values and ValueOf down into the subclasses, but I was hoping to keep all the complexity in the superclass and keep the boilerplate to a minimum. Can anyone whose C#-fu is superior to mine come up with a trick for making the subclass instances “self-executing”?


Note: FWIW, this is in Mono, on Mac OS. YM in MS .NET, on Windows, MV.


ETA: For monoglot C# developers (or even polyglot developers whose experience is limited to languages starting with ‘C’) wondering WTF I’m trying to do: this. C# enums take care of the type safety issue, but they’re still missing everything else.

  • 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-20T11:20:22+00:00Added an answer on May 20, 2026 at 11:20 am

    I came up with this – not entirely pleasing, but does do the job:

            public static IEnumerable<T> Values
            {
                get
                {
                    if (nameRegistry.Count > 0)
                    {
                        return nameRegistry.Values;
                    }
                    var aField = typeof (T).GetFields(
                                            BindingFlags.Public | BindingFlags.Static)
                                        .FirstOrDefault();
    
                    if (aField != null)
                        aField.GetValue(null);
    
                    return nameRegistry.Values;
                }
            }
    

    EDIT Here’s a slightly different version that should address VinayC’s concerns in the comments. The problem was this: thread A calls Values(). While the static constructor of SomeEnum is running, after it’s added V1 but before it adds V2, thread B calls values. In the code as originally written, it would be handed an IEnumerable that might only yield V1. So you could get incorrect results from Values() if a second thread calls during the very first call to Values() for any particular type.

    The version below uses a boolean flag rather than relying on a non-zero count in nameRegistry. In this version it is still possible that the reflection code to run more than once, but no longer possible to get wrong answers from Values(), since by the time the reflection code completes, the nameRegistry is guaranteed to be fully initialized.

            private static bool _initialized;
            public static IEnumerable<T> Values
            {
                get
                {
                    if (_initialized)
                    {
                        return nameRegistry.Values;
                    }
                    var aField = typeof(T).GetFields(
                                                BindingFlags.Public | BindingFlags.Static)
                                            .FirstOrDefault();
                    if (aField != null)
                        aField.GetValue(null);
                    _initialized = true;
                    return nameRegistry.Values;
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand from the answer to this question that values of global/static uninitialized int
From web pages like this one, http://www.jsftutorials.net/components/step5.html I understand that the binding attribute in
This answer hit a nerve with me on something I have never understood with
I have class instantiated in a web service that, in a static member, holds
The section $7.3.1.1/2 from the C++ Standard reads: The use of the static keyword
I used this guide , but the difference is that I don't want to
I think the answer to this question is so obivous that noone has bothered
I understood that this is a reference to the currently executing object. If that
I'm trying to understand this line from an strace on linux: sendto(10, \24\0\0\0\26\0\1\3\233\274\362O\0\0\0\0\0\0\0\0, 20,
I try to understand this example from jquery api in this snippet var tags

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.