Enums Class
Definition
Provides type-level helpers for enumerations: cached value and name lookups, parsing, and resolution of values from their DescriptionAttribute or DisplayAttribute text.
public static class Enums
- Inheritance
-
Enums
- Inherited Members
Remarks
Value and name lookups are served from a per-enum cache populated once on first use and are backed by the trimmer-friendly intrinsics GetValues<TEnum>() and GetNames<TEnum>(). Description and display-name lookups read the enum's field attributes by reflection; this is trim- and ahead-of-time-safe because enum fields and the referenced attribute types are always preserved, so no RequiresUnreferencedCodeAttribute is required.
Methods
GetNames<TEnum>()
Returns the names declared by the specified enumeration.
public static string[] GetNames<TEnum>() where TEnum : struct, Enum
Returns
- string[]
A fresh array of the names declared by
TEnum.
Type Parameters
TEnumThe enumeration type.
Remarks
A new array is returned on every call so callers may mutate the result without affecting the internal cache or other callers, matching the behavior of GetNames<TEnum>(). The underlying reflection result is cached, so repeated calls only pay the cost of a single array copy.
GetValues<TEnum>()
Returns the values declared by the specified enumeration.
public static TEnum[] GetValues<TEnum>() where TEnum : struct, Enum
Returns
- TEnum[]
A fresh array of the values declared by
TEnum.
Type Parameters
TEnumThe enumeration type.
Remarks
A new array is returned on every call so callers may mutate the result without affecting the internal cache or other callers, matching the behavior of GetValues<TEnum>(). The underlying reflection result is cached, so repeated calls only pay the cost of a single array copy.
TryParseDescription<TEnum>(string?, out TEnum)
Attempts to resolve the enumeration value whose DescriptionAttribute or DisplayAttribute text matches the specified text.
public static bool TryParseDescription<TEnum>(string? text, out TEnum value) where TEnum : struct, Enum
Parameters
textstringThe description or display text to resolve.
valueTEnumWhen this method returns, contains the matching value, or the default value on failure.
Returns
Type Parameters
TEnumThe enumeration type.
Remarks
Matching is ordinal. When more than one value declares the same description or display text, the first declaring value in ascending underlying-value order wins; the lookup is therefore deterministic.
TryParse<TEnum>(string?, bool, out TEnum)
Attempts to parse the specified text into a value of the enumeration, optionally ignoring case.
public static bool TryParse<TEnum>(string? text, bool ignoreCase, out TEnum value) where TEnum : struct, Enum
Parameters
textstringThe text to parse.
ignoreCaseboolvalueTEnumWhen this method returns, contains the parsed value, or the default value on failure.
Returns
Type Parameters
TEnumThe enumeration type.
TryParse<TEnum>(string?, out TEnum)
Attempts to parse the specified text into a value of the enumeration.
public static bool TryParse<TEnum>(string? text, out TEnum value) where TEnum : struct, Enum
Parameters
textstringThe text to parse.
valueTEnumWhen this method returns, contains the parsed value, or the default value on failure.
Returns
Type Parameters
TEnumThe enumeration type.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |