Table of Contents

Enums Class

Definition

Namespace
Bodu.Extensions
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
Enums.cs

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

TEnum

The 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

TEnum

The 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

text string

The description or display text to resolve.

value TEnum

When this method returns, contains the matching value, or the default value on failure.

Returns

bool

true if a matching value was found; otherwise, false.

Type Parameters

TEnum

The 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

text string

The text to parse.

ignoreCase bool

true to ignore case; otherwise, false.

value TEnum

When this method returns, contains the parsed value, or the default value on failure.

Returns

bool

true if parsing succeeded; otherwise, false.

Type Parameters

TEnum

The 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

text string

The text to parse.

value TEnum

When this method returns, contains the parsed value, or the default value on failure.

Returns

bool

true if parsing succeeded; otherwise, false.

Type Parameters

TEnum

The enumeration type.

Applies to

ProductVersions
.NET8, 10