EnumComparerTEnum Class

Provides an efficient IEqualityComparerT and IComparerT implementation for Enum types. Can be used for example in DictionaryTKey, TValue, SortedListTKey, TValue or CacheTKey, TValue instances with enum key, or as a comparer for List<T>.Sort(IComparer<T>) method to sort enum elements.

Definition

Namespace: KGySoft.CoreLibraries
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 7.0.0
C#
[SerializableAttribute]
[CLSCompliantAttribute(false)]
public abstract class EnumComparer<TEnum> : IEqualityComparer<TEnum>, 
	IComparer<TEnum>, ISerializable
Inheritance
Object    EnumComparerTEnum
Implements
IComparerTEnum, IEqualityComparerTEnum, ISerializable

Type Parameters

TEnum
The type of the enumeration. Must be an Enum type.

Remarks

Using dictionaries with enum key and finding elements in an enum array works without using EnumComparerTEnum, too. But unlike Int32 or the other possible underlying types, enum types does not implement the generic IEquatableT and IComparableT interfaces. This causes that using an enum as key in a dictionary, for example, can be very ineffective (depends on the used framework, see the note below) due to heavy boxing and unboxing to and from Object type. This comparer generates the type specific IEqualityComparer<TEnum>.Equals, IEqualityComparer<T>.GetHashCode and IComparer<T>.Compare methods for any enum type.

  Note

The optimization of EqualityComparerT and ComparerT instances for enum types may differ in different target frameworks.
  • In .NET Framework 3.5 and earlier versions they are not optimized at all.
  • In .NET 4.0 Framework EqualityComparerT was optimized for Int32-based enums. (Every .NET 4.0 assembly is executed on the latest 4.x runtime though, so this is might be relevant only on Windows XP where no newer than the 4.0 runtime can be installed.)
  • In latest .NET 4.x Framework versions EqualityComparerT is optimized for any enum type but ComparerT is not.
  • In .NET Core both EqualityComparerT and ComparerT are optimized for any enum types so the performance benefit of using EnumComparerTEnum in .NET Core is negligible.

  Note

In .NET Standard 2.0 building dynamic assembly is not supported so the .NET Standard 2.0 version falls back to use EqualityComparerT and ComparerT classes.

Example

Example for initializing of a DictionaryTKey, TValue with EnumComparerTEnum:
C#
Dictionary<MyEnum, string> myDict = new Dictionary<MyEnum, string>(EnumComparer<MyEnum>.Comparer);

Constructors

EnumComparerTEnum Protected constructor to prevent direct instantiation.

Properties

Comparer Gets the comparer instance for TEnum type.

Methods

Compare Compares two TEnum instances and returns a value indicating whether one is less than, equal to, or greater than the other.
Equals(Object) Determines whether the specified obj is the same type of EnumComparerTEnum as the current instance.
(Overrides ObjectEquals(Object))
Equals(TEnum, TEnum) Determines whether two TEnum instances are equal.
GetHashCode Returns a hash code for this instance.
(Overrides ObjectGetHashCode)
GetHashCode(TEnum) Returns a hash code for the specified TEnum instance.
ToEnum Converts an ulong value to TEnum.
ToInt64 Converts a TEnum value to long.
ToUInt64 Converts a TEnum value to ulong.

Extension Methods

Convert Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions)
ConvertTTarget Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions)
In Gets whether item is among the elements of set.
See the Examples section of the generic InT(T, T) overload for an example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)

See Also