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