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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:06:44+00:00 2026-06-16T16:06:44+00:00

This isn’t really an optimization question or anything. It’s essentially a what-the-heck-is-going-on-here kind of

  • 0

This isn’t really an optimization question or anything. It’s essentially a what-the-heck-is-going-on-here kind of question. The singleton pattern is used to have a single instance of an object used multiple times. That’s all well and good, but if I try to do a similar pattern with a struct, it don’t get a single instance.

I came about this trying to do something with the Color struct in System.Drawing. Here is some example code:

    class Colors
    {
        private static Color _red;
        public static Color Red
        {
            get
            {
                if (_red.IsEmpty)
                    _red = Color.FromArgb(0xFF, 0xFF, 0x42, 0x39);

                return _red;
            }
        }
    }

    static void Main(string[] args)
    {
        var redOne = Colors.Red;
        var redTwo = Colors.Red;

        Console.WriteLine("redOne.Equals(redTwo) : {0}", redOne.Equals(redTwo));
        Console.WriteLine("redOne == redTwo : {0}", redOne == redTwo);
        Console.WriteLine("Object.Equals(redOne, redTwo) : {0}", Object.Equals(redOne, redTwo));
        Console.WriteLine("Object.ReferenceEquals(redOne, redTwo) : {0}", Object.ReferenceEquals(redOne, redTwo));
        Console.ReadLine();
    }

The output for this is:

redOne.Equals(redTwo) : True
redone == redTwo : True
Object.Equals(redOne, redTwo) : True
Object.ReferenceEquals(redOne, redTwo) : False

The first three results are to be expected, but the last is what surprised me. Now, my best guess is that when _red is returned from Colors.Red it’s returning a copy, as normal value types do. So, while there is only a single instance of _red, Colors.Red returns a brand new instance, and that is what gets stored into redOne and redTwo. Is my thinking correct?

Also, if that is correct, is there any point in using the static keyword on structs?

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-06-16T16:06:45+00:00Added an answer on June 16, 2026 at 4:06 pm

    ReferenceEquals will not work with a struct/value type, only a class.

    Calling ReferenceEquals on two structs will ‘box’ each struct into it’s own memory location and then compare the two (obviously) different addresses.

    Since a class is already a reference type, the call to ReferenceEquals compares the actual address of the instance.

    Essentially you do not need to store a static copy of ‘Red’. Change the code to the following:

    class Colors
    {
        public static Color Red
        {
            get
            {
                return Color.FromArgb(0xFF, 0xFF, 0x42, 0x39);
            }
        }
    }
    

    Additional reading:
    MSDN – Structs (C# Programming Guide)

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

Sidebar

Related Questions

This isn't really a technical question, but I essentially have two sites, a mobile
This isn't strictly a programming question, but I'm asking it here because it's certainly
This isn't really a technical question so I apolgoise if I ruffle any feathers!
This isn't much of a coding question as opposed to seeking help on going
This isn't really a programming question... Are there any ways to determine which application
This isn't strictly a programming question, but hopefully someone here has the knowledge to
This isn't really a question on a problem; I've solved it my own (after
This isn't the best question ever, but since search engines feel the need to
This isn't a question that I am expecting a specific answer to since it
This isn't meant as a troll or flamebait or anything like that. I've been

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.