EnumComparerTEnum Class
Namespace: KGySoft.CoreLibrariesAssembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0-preview.1
[SerializableAttribute]
[CLSCompliantAttribute(false)]
public abstract class EnumComparer<TEnum> : IEqualityComparer<TEnum>,
IComparer<TEnum>, ISerializable
<SerializableAttribute>
<CLSCompliantAttribute(false)>
Public MustInherit Class EnumComparer(Of TEnum)
Implements IEqualityComparer(Of TEnum), IComparer(Of TEnum),
ISerializable
[SerializableAttribute]
[CLSCompliantAttribute(false)]
generic<typename TEnum>
public ref class EnumComparer abstract : IEqualityComparer<TEnum>,
IComparer<TEnum>, ISerializable
[<AbstractClassAttribute>]
[<SerializableAttribute>]
[<CLSCompliantAttribute(false)>]
type EnumComparer<'TEnum> =
class
interface IEqualityComparer<'TEnum>
interface IComparer<'TEnum>
interface ISerializable
end
- Inheritance
- Object EnumComparerTEnum
- Implements
- IComparerTEnum, IEqualityComparerTEnum, ISerializable
Type Parameters
- TEnum
- The type of the enumeration. Must be an Enum type.
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.
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 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.
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 for initializing of a
DictionaryTKey, TValue with
EnumComparerTEnum:
Dictionary<MyEnum, string> myDict = new Dictionary<MyEnum, string>(EnumComparer<MyEnum>.Comparer);
Comparer |
Gets the comparer instance for TEnum type.
|