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.0.0-rc.1
Syntax

[SerializableAttribute]
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.
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Returns a hash code for this instance.
(Overrides ObjectGetHashCode.)
Public methodGetHashCode(TEnum)
Returns a hash code for the specified TEnum instance.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
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