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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:17:06+00:00 2026-05-13T00:17:06+00:00

I’m trying to use the code from this article: Creating Fake Enums , but

  • 0

I’m trying to use the code from this article: Creating Fake Enums, but I can’t figure out why it doesn’t work.

This code:

Console.WriteLine(FakeEnum.One.FriendlyName);
Console.WriteLine(FakeEnum.Four.FriendlyName);

generates an exception:

System.TypeInitializationException was unhandled
  Message="The type initializer for 'FakeEnum' threw an exception."
  Source="FakeEnum1"
  TypeName="FakeEnum"
  StackTrace:
       at FakeEnum1.Program.Main(String[] args) in ..\Test\FakeEnum1\Program.cs:line 26
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.NullReferenceException
       Message="Object reference not set to an instance of an object."
       Source="FakeEnum1"
       StackTrace:
            at FakeEnum.op_Equality(FakeEnum a, FakeEnum b) in ..\Test\FakeEnum1\FakeEnum.cs:line 158
            at FakeEnum.ToString(String format) in ..\Test\FakeEnum1\FakeEnum.cs:line 31
            at FakeEnum.ToString() in ..\Test\FakeEnum1\FakeEnum.cs:line 25
            at FakeEnum..ctor(Int32 value, String friendlyName) in ..\Test\FakeEnum1\FakeEnum.cs:line 171
            at FakeEnum..ctor(Int32 value) in ..\Test\FakeEnum1\FakeEnum.cs:line 165
            at FakeEnum..cctor() in ..\Test\FakeEnum1\FakeEnum.cs:line 13

If I comment the members declared with the (int) constructor, everything else works:

public static readonly FakeEnum One = new FakeEnum(1, "One's Friendly Name");
public static readonly FakeEnum Two = new FakeEnum(2, "Two's Friendly Name");
public static readonly FakeEnum Three = new FakeEnum(3, "Three's Friendly Name");
//public static readonly FakeEnum Four = new FakeEnum(4);
//public static readonly FakeEnum Five = new FakeEnum(5);
//public static readonly FakeEnum Six = new FakeEnum(6);

Now, if I make the constructors public, the following code works just fine:

FakeEnum a = new FakeEnum(14, "1 4");
FakeEnum b = new FakeEnum(28);
Console.WriteLine(a.FriendlyName);
Console.WriteLine(b.FriendlyName);

I just run out of ideas – what am I missing, and what generates the exception when using the original code?

  • 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-13T00:17:06+00:00Added an answer on May 13, 2026 at 12:17 am

    It’s this bit:

    FakeEnum temp = staticField.GetValue(null) as FakeEnum;
    if(temp == null) continue;
    

    That “==” is calling the == operator, which doesn’t expect the left hand side to be null:

    public static bool operator == (FakeEnum a, FakeEnum b) { 
        return a.Equals(b);
    }
    

    You can fix the operator like this:

    public static bool operator == (FakeEnum a, FakeEnum b) { 
        if (object.ReferenceEquals(a, b)) {
            return true;
        }
        if (object.ReferenceEquals(a, null) || object.ReferenceEquals(b, null)) {
            return false;
        }
        return a.Equals(b);
    }
    

    (Or by using object.Equals, as pointed out in another answer – doh!)

    or you could change it to make Equals delegate to == instead of the other way round.

    Personally I’d say this class is looking a tad hairy. It’s not even clear to me why == and != need overloading or indeed why Equals needs overriding unless you really have two separate objects with the same value (e.g. due to serialization, which is going to be icky anyway).

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

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You shouldn't have to unserialize anything in PHP from the… May 13, 2026 at 1:16 am
  • Editorial Team
    Editorial Team added an answer You can use multiple where clauses which will be ANDed… May 13, 2026 at 1:16 am
  • Editorial Team
    Editorial Team added an answer That's because you haven't actually saved the file as UTF-8.… May 13, 2026 at 1:16 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.