سؤال

Perhaps this question sounds silly, but why does a generic and a non-generic IComparable interface exist?

Furthermore, which one is prefered to use and why?

هل كانت مفيدة؟

المحلول

The non-generic IComparable was added in version 1.1 before generics were introduced, while IComparer<T> was added alongside generics in version 2.0.

The generic version is preferred for the same reason as all generic interfaces - it is safer and documents the intent more clearly. Struct types also do not need to be boxed when passed to a generic method, as they would be for one with an object argument like IComparable.CompareTo so there is also a performance benefit.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top