I could write the following to convert an object to an integer.
Convert.ToInt32(myObject);
But I could also write
Int.Parse
According to the documentation, it would depend on the object and whether or not it implements the IConvertible interface. There are a number of reasons that make these approaches different. Notably, if the string representation doesn't represent the corresponding integer value (e.g., "{ Value = 123 }") or the object isn't IConvertible
. I would choose using Convert.ToInt32()
as the conversion is defined by the type and not relying on some observed property that could change in the future.