How can I check whether a struct has been instantiated?
A struct
is a value type - it's never null.
You can check against default(ProportionPoint)
, which is the default value of the struct (e.g. zero). However, for a point, it may be that the default value - the origin - is also a "valid" value.
Instead you could use a Nullable<ProportionPoint>
.
because p is struct it never be null so you should compare it to it's default value. In order to check equivalence between your value and dafault value. If you use == you will get
cannot be applied to operands of type 'ProportionPoint' and 'ProportionPoint' error
because structs do not get an implementation of == by default. so you need to overload the == and != operators in your struct like this:
public static bool operator ==(firstOperand op1, secondOperand2 op2)
{
return op1.Equals(op2);
}
public static bool operator !=(firstOperand op1, secondOperand2 op2)
{
return !op1.Equals(op2);
}
and then :
if (this.p == default(ProportionPoint))
another option is to use Equals directly:
f (this.p.Equals.default(ProportionPoint))
structs are value types and they can never be null contrary to reference types. You could check against the default value:
if (this.p.Equals(default(ProportionPoint)))
A struct can't be null. Its a value type, not a reference type. You need to check against the properties with the default values. Something like:
if(p.X == 0 && p.Y == 0)