For some classes, ideally, I would like to create special named instances similar to "null". As far as I know, this is not possible, so instead I create static instances of the class with a static constructor similar to this:
public class Person { public static Person Waldo;
As you can see, Person.Waldo is a special instance of the Person class that I created because there are many other classes in my program that might want to refer to this special well-known instance.
The disadvantage of implementing this method is that I do not know how to make all the properties of Person.Waldo immutable, while all the properties of a "normal" instance of Person must be changed. Whenever I accidentally have a Person object related to Waldo, and I do not carelessly check to see if it references Waldo, then I accidentally clobber the Waldo property.
Is there a better way or even some additional alternatives for defining special known instances of a class?
The only solution I know right now is to implement get and set accessors and check "if (this == Waldo) throw new ..." on each set. Although this works, I suggest that C # can do better than me to implement it. If only I can find some C # way to make all Waldo properties readonly (except for the static constructor.)
source share