I know that I can avoid boxing by adding my own implementation of Equals.
public struct TwoDoubles { public double m_Re; public double m_Im; public TwoDoubles(double one, double two) { m_Re = one; m_Im = two; } public override bool Equals(object ob) { return Equals((TwoDoubles)ob); } public bool Equals(TwoDoubles ob) { TwoDoubles c = ob; return m_Re == c.m_Re && m_Im == c.m_Im; } }
I can't call it overriding as much as overloading. By the magic of the runtime, it correctly returns the correct Equals() implementation based on the type of the caller.
Why can't I override and change the type of the parameter to TwoDoubles and let the box by the power of the runtime as needed? This is because C # does not support the parameterization of contravariance (if the reason is not supported for some reason ... it seems like a small step from object o = new TwoDoubles() )?
UPDATE
Explanation: object is part of the structure inheritance hierarchy. Why can't we specify a more derived type as a parameter to override an implementation from a less derived type? This would allow us to write:
public override bool Equals(TwoDoubles ob) { TwoDoubles c = ob; return m_Re == c.m_Re && m_Im == c.m_Im; }
Which should be called when the variable is TwoDouble, even if the specified variable was placed in an object of type.
c #
P.Brian.Mackey
source share