I know it's quite a late reply, but for what it's worth here is my implementation.
You will have to:
- Create a private
bool property called isEmpty - Create a private constructor with a
bool argument that assigns it to isEmpty - Create a public static getter property called
Empty which returns an instance of your object and calls the private constructor passing a true argument - Overload two operators
== and != to check if isEmpty is true on both
Example
Let's say I have a class called ExampleClass:
public class Example { public string Message; private bool isEmpty; public Example(string Message) { this.Message = Message; isEmpty = false; } }
The bool property called isEmpty has been defined, as well as a string property and a basic Constructor to assign to the string.
A private constructor also needs to be defined which takes one boolean argument and assigns it to isEmpty.
private Example(bool isEmpty) { this.isEmpty = isEmpty; }
You will then need to define a static getter, which in this example returns a new instance of Example by calling the private constructor and assigning isEmpty to true.
public static Example Empty { get { return new Example(isEmpty: true); } }
Once you've done that, you need to overload two operators. The equals and not equals. If you don't overload these two operators you may get an error when you try to check if an instance is equal to Example.Empty.
public static bool operator ==(Example eg1, Example eg2) { if (eg1.isEmpty == true && eg2.isEmpty == true) return true; else return Example.Equals(eg1, eg2); } public static bool operator !=(Example eg1, eg2) { if (eg1.isEmpty != eg2.isEmpty) return true; else return !Example.Equals(eg1,eg2); }