KGy SOFT

EnumComparerTEnum Class

KGy SOFT Core Libraries Help
Inheritance Hierarchy

SystemObject
  KGySoft.CoreLibrariesEnumComparerTEnum

Namespace:  KGySoft.CoreLibraries
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.3.0
Syntax

[SerializableAttribute]
[CLSCompliantAttribute(false)]
public abstract class EnumComparer<TEnum> : IEqualityComparer<TEnum>, 
	IComparer<TEnum>, ISerializable

Type Parameters

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

The EnumComparerTEnum type exposes the following members.

Constructors

  NameDescription
Protected methodEnumComparerTEnum
Protected constructor to prevent direct instantiation.
Top
Properties

  NameDescription
Public propertyStatic memberComparer
Gets the comparer instance for TEnum type.
Top
Methods

  NameDescription
Public methodCompare
Compares two TEnum instances and returns a value indicating whether one is less than, equal to, or greater than the other.
Public methodEquals(Object)
Determines whether the specified obj is the same type of EnumComparerTEnum as the current instance.
(Overrides ObjectEquals(Object).)
Public methodEquals(TEnum, TEnum)
Determines whether two TEnum instances are equal.
Public methodGetHashCode
Returns a hash code for this instance.
(Overrides ObjectGetHashCode.)
Public methodGetHashCode(TEnum)
Returns a hash code for the specified TEnum instance.
Protected methodToEnum
Converts an ulong value to TEnum.
Protected methodToInt64
Converts a TEnum value to long.
Protected methodToUInt64
Converts a TEnum value to ulong.
Top
Extension Methods

  NameDescription
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
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.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
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.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
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.)
Top
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 Note
The optimization of EqualityComparerT and ComparerT instances for enum types may differ in different target frameworks.
Examples

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

Reference