Enum based Singleton
This is the description for my job training task:
I don't understand how to implement this. I find myself coding the same old implementation with a static backing field.
Not here to get my homework done. I'm genuinely trying to learn
8 Replies
What is "the Enum approach"?
Also, a solution to singleton without lazy or locking is to simply instantiate eagerly a static field/property and hide the constructor of your class.
they probably mean constants wrapped in a class
static constructors are thread safe afaik
so yeah, that's probably the anwer
I assume that's referring to something earlier in the training material?
That's what I don't understand... I kinda have no one to ask what's to be done
oh okay its actually a thing. I found java implementations online:
Java enums are very different to C# enums
You can't do that in C#
yea exactly
im unable to find any ACTUAL implementation
I found this C# based on on a website:
but I don't think the enum plays any role here
Yes, someone copied some Java docs, and didn't update them for C#
Well java enum singletons seem to be simple static readonly fields/props that are initialized eagerly to their value.