I was looking at the documentation for BigInteger and ran across the following in the BigInteger.Equals(Object obj) method:
If the obj parameter is not a BigInteger value, but it is a data type for which an implicit conversion is defined, the Equals(Object) method converts obj to a BigInteger value before it performs the comparison.
I then started wondering how I would go about doing this, given that converting from the object would require an explicit cast. The best way I came up with is as follows:
Type type = obj.GetType()
if(type == typeof(byte))
{
byte b = (byte)obj;
return Equals(b);
}
if(type == typeof(short))
{
short s = (short)obj;
return Equals(s);
}
//Continue for all types that can be implicitly cast to BigInteger.
It just seems to me that there’s an easier way to do this that I’m missing. So what other methods are there to do an implicit conversion, given that I’m starting with an object?
The current implementation from ILSpy:
At the very least the documentation could be considered misleading in its wording. What it means is
BigIntegeror anything that derives from it will have an implicit cast toBigIntegerand will thus be cast to aBigIntegerfor the purposes of testing equality.The current definition of
structwill mean that nothing can derive fromBigInteger, so the documentation is actually a little pointless in that statement.It is possible you are getting confused with the user-defined
implicitoperator, which is what will have been used to allowint,short, et al to be stored inBigInteger“implicitly” in code: