I have an addition to what others have said.
1) Implement a public instance method "IsEmpty()"
2) Implement a public static property "Empty"
but also...
3) store a private bool field _bEmpty. IsEmpty() returns _bEmpty, and
"Empty" creates new instance with _bEmpty = true, and all other values
initialized to defaults.
Once a value type variable is initialized, it can't be uninitialized.
reference types can be set to null, but not value types. Just try setting
an integer to null. Also how do you check if a value type is initialized?
Just try this...
public struct Something
{
private int i;
public bool IsEmpty()
{
if (i == null) //compile error...
// "attempt to use before initialized" (not trapable exception)
{return true;}else{return false;}
}
}
Therefore depending on a value being uninitialized does not work.
A structure such as Point has no set of values for X and Y that would imply
emptiness. 0,0 or any other set of numbers is valid. Then any method that
returns your type can return one with _bEmpty of true when a valid return
value does not exist.
However, don't overuse empty on every kind of object. It may make more
sense to return null instead of a class instance that is "empty". Also null
uses less memory. Only, add "Empty" to a struct/class where object "state"
is important.
Michael Lang, MCSD