Search code examples
c#objectcompareto

Object.CompareTo(Object) for unknown data type


I am trying to compare objects in an object[] that are of a single type (unknown at runtime). They are of System.string, int, decimal, Datetime, or bool types.

Is there a way to compare two of these objects to determine if one is greater or less than another without having to cast them into their appropriate type first?


Solution

  • The types in question all implement IComparable, so, if being able to compare elements is an intrinsic requirement of your array, you could declare it as an IComparable[] instead.