### Installing FastEnum via NuGet Source: https://github.com/xin9le/fastenum/blob/main/README.md Command to add the FastEnum package to a .NET project. ```bash dotnet add package FastEnum ``` -------------------------------- ### GetMinValue / GetMaxValue - Get Boundary Values Source: https://context7.com/xin9le/fastenum/llms.txt Returns the minimum or maximum value defined in the enumeration. ```APIDOC ## GetMinValue / GetMaxValue ### Description Returns the minimum or maximum value defined in the enumeration, or null if the enum is empty. ### Response #### Success Response (200) - **value** (T?) - The boundary value or null. ``` -------------------------------- ### GetMembers - Get Member Information Source: https://context7.com/xin9le/fastenum/llms.txt Retrieves an array of Member objects containing detailed information about each enum constant. ```APIDOC ## GetMembers ### Description Retrieves an array of Member objects containing detailed information about each enum constant including value, name, and field info. ### Response #### Success Response (200) - **members** (Array>) - List of objects containing Name, Value, and FieldInfo. ``` -------------------------------- ### Get All Enum Names with FastEnum Source: https://context7.com/xin9le/fastenum/llms.txt Obtain an immutable array of all defined enum names. This is helpful for displaying or processing the names of enum members. ```csharp using FastEnumUtility; enum HttpMethod { Get, Post, Put, Delete } // Get all enum names as ImmutableArray var names = FastEnum.GetNames(); Console.WriteLine($"Available methods: {string.Join(", ", names)}"); // Output: Available methods: Get, Post, Put, Delete ``` -------------------------------- ### Get Enum Member Information Source: https://context7.com/xin9le/fastenum/llms.txt Retrieve an array of Member objects using FastEnum.GetMembers. Each object contains detailed information about enum constants, including their name, value, and FieldInfo. ```csharp using FastEnumUtility; enum UserRole { Guest = 0, User = 1, Admin = 2, SuperAdmin = 3 } // Get all members with full metadata var members = FastEnum.GetMembers(); foreach (var member in members) { Console.WriteLine($"Name: {member.Name}, Value: {(int)member.Value}, Type: {member.FieldInfo.FieldType}"); } // Output: // Name: Guest, Value: 0, Type: UserRole // Name: User, Value: 1, Type: UserRole // Name: Admin, Value: 2, Type: UserRole // Name: SuperAdmin, Value: 3, Type: UserRole ``` -------------------------------- ### Get All Enum Values with FastEnum Source: https://context7.com/xin9le/fastenum/llms.txt Retrieve an immutable array of all defined enum values with zero allocation after initial caching. This is useful for iterating through all possible states of an enum. ```csharp using FastEnumUtility; enum Fruits { Apple = 1, Lemon = 2, Melon = 4, Banana = 8 } // Get all enum values as ImmutableArray var values = FastEnum.GetValues(); foreach (var fruit in values) { Console.WriteLine($"Value: {fruit} = {(int)fruit}"); } // Output: // Value: Apple = 1 // Value: Lemon = 2 // Value: Melon = 4 // Value: Banana = 8 ``` -------------------------------- ### Get Minimum and Maximum Enum Values Source: https://context7.com/xin9le/fastenum/llms.txt Retrieve the minimum or maximum defined value in an enumeration using FastEnum.GetMinValue and FastEnum.GetMaxValue. Returns null if the enum is empty. ```csharp using FastEnumUtility; enum Temperature { Freezing = -10, Cold = 0, Warm = 20, Hot = 35 } // Get min and max values var min = FastEnum.GetMinValue(); var max = FastEnum.GetMaxValue(); Console.WriteLine($"Min: {min} ({(int?)min})"); Console.WriteLine($"Max: {max} ({(int?)max})"); // Output: // Min: Freezing (-10) // Max: Hot (35) ``` -------------------------------- ### GetEnumMemberValue - Get EnumMemberAttribute Value Source: https://context7.com/xin9le/fastenum/llms.txt Retrieves the value from an EnumMemberAttribute attached to an enum field. ```APIDOC ## GetEnumMemberValue ### Description Retrieves the value from an EnumMemberAttribute attached to an enum field, commonly used for serialization aliases. ### Parameters #### Query Parameters - **throwIfNotFound** (bool) - Optional - Whether to throw an exception if the attribute is not found. ### Response #### Success Response (200) - **value** (string?) - The string value defined in the EnumMemberAttribute. ``` -------------------------------- ### Getting Pairwised Enum Member Information Source: https://github.com/xin9le/fastenum/blob/main/README.md Retrieves detailed information about enum members, including their value, name, and FieldInfo, using the ToMember() extension method. Supports deconstruction for easy access to name and value. ```cs class Member { public TEnum Value { get; } public string Name { get; } public FieldInfo FieldInfo { get; } // etc... } var member = Fruits.Apple.ToMember()!; var (name, value) = member; // Supports deconstruction ``` -------------------------------- ### Getting EnumMemberAttribute Value Source: https://github.com/xin9le/fastenum/blob/main/README.md Quickly obtains the value from an EnumMemberAttribute applied to an enum field. This is useful when the attribute is used as an alias for the field name. ```cs enum Company { [EnumMember(Value = "Apple, Inc.")] Apple = 0, } var value = Company.Apple.GetEnumMemberValue(); // Apple, Inc. ``` -------------------------------- ### Get EnumMemberAttribute Value Source: https://context7.com/xin9le/fastenum/llms.txt Retrieve the value from an EnumMemberAttribute using GetEnumMemberValue. This is useful for serialization aliases. The method supports safe retrieval without throwing if the attribute is not found. ```csharp using System.Runtime.Serialization; using FastEnumUtility; enum Company { [EnumMember(Value = "Apple, Inc.")] Apple = 0, [EnumMember(Value = "Microsoft Corporation")] Microsoft = 1, [EnumMember(Value = "Alphabet Inc.")] Google = 2 } // Get the EnumMember value string? appleName = Company.Apple.GetEnumMemberValue(); Console.WriteLine($"Apple: {appleName}"); // Output: Apple: Apple, Inc. string? msName = Company.Microsoft.GetEnumMemberValue(); Console.WriteLine($"Microsoft: {msName}"); // Output: Microsoft: Microsoft Corporation // Safe retrieval without throwing string? safeName = Company.Google.GetEnumMemberValue(throwIfNotFound: false); Console.WriteLine($"Google: {safeName}"); // Output: Google: Alphabet Inc. ``` -------------------------------- ### Get Name of Enum Value with FastEnum Source: https://context7.com/xin9le/fastenum/llms.txt Retrieve the string name for a specific enum value. Returns null if the value is not defined within the enum, providing a safe way to look up names. ```csharp using FastEnumUtility; enum Status { Pending = 0, Active = 1, Completed = 2 } // Get name from defined value string? name = FastEnum.GetName(Status.Active); Console.WriteLine($"Name: {name}"); // Output: Name: Active // Get name from undefined value returns null string? undefinedName = FastEnum.GetName((Status)99); Console.WriteLine($"Undefined: {undefinedName ?? "null"}"); // Output: Undefined: null ``` -------------------------------- ### GetUnderlyingType - Get Enum's Underlying Type Source: https://context7.com/xin9le/fastenum/llms.txt Retrieves the underlying numeric type of a specified enumeration (e.g., `Int32`, `Byte`, `Int64`). This method is useful for understanding the data type used to store the enum values. ```csharp using FastEnumUtility; enum DefaultEnum { A, B } // Default underlying type is int enum ByteEnum : byte { X, Y } enum LongEnum : long { P, Q } Type defaultType = FastEnum.GetUnderlyingType(); Console.WriteLine($"DefaultEnum underlying type: {defaultType.Name}"); // Output: DefaultEnum underlying type: Int32 Type byteType = FastEnum.GetUnderlyingType(); Console.WriteLine($"ByteEnum underlying type: {byteType.Name}"); // Output: ByteEnum underlying type: Byte Type longType = FastEnum.GetUnderlyingType(); Console.WriteLine($"LongEnum underlying type: {longType.Name}"); // Output: LongEnum underlying type: Int64 ``` -------------------------------- ### FastEnum vs. System.Enum Usage Source: https://github.com/xin9le/fastenum/blob/main/README.md Demonstrates the direct replacement of System.Enum methods with FastEnum equivalents for common enum operations. Use FastEnum for improved performance. ```cs //--- FastEnum var values = FastEnum.GetValues(); var names = FastEnum.GetNames(); var name = FastEnum.GetName(Fruits.Apple); var toString = Fruits.Apple.FastToString(); var defined = FastEnum.IsDefined(Fruits.Apple); var parse = FastEnum.Parse("Apple"); var tryParse = FastEnum.TryParse("Apple", out var value); ``` ```cs //--- .NET var values = Enum.GetValues(); var names = Enum.GetNames(); var name = Enum.GetName(Fruits.Apple); var toString = Fruits.Apple.ToString(); var defined = Enum.IsDefined(Fruits.Apple); var parse = Enum.Parse("Apple"); var tryParse = Enum.TryParse("Apple", out var value); ``` -------------------------------- ### Source Code Generation for Maximum Performance Source: https://context7.com/xin9le/fastenum/llms.txt Achieve maximum performance in mission-critical scenarios by using source code generation. Define a booster class with the `FastEnum` attribute and then use `FastEnum` APIs with the booster for compile-time optimized code. ```csharp using System.Net; using FastEnumUtility; // Step 1: Define a booster class with FastEnum attribute [FastEnum] partial class HttpStatusCodeBooster { } // Step 2: Use the booster with FastEnum APIs for maximum performance public class HttpHandler { public void ProcessResponse(HttpStatusCode statusCode) { // High-performance ToString using source generation string name = FastEnum.ToString(statusCode); Console.WriteLine($"Status: {name}"); // High-performance IsDefined check bool isDefined = FastEnum.IsDefined(statusCode); Console.WriteLine($"Is valid status: {isDefined}"); // High-performance parsing if (FastEnum.TryParse("OK", out var parsed)) { Console.WriteLine($"Parsed: {parsed}"); } } } // Example usage: // var handler = new HttpHandler(); // handler.ProcessResponse(HttpStatusCode.OK); // Output: // Status: OK // Is valid status: True // Parsed: OK ``` -------------------------------- ### Check Enum Properties (Continuous, Flags, Empty) Source: https://context7.com/xin9le/fastenum/llms.txt Utilize FastEnum.IsContinuous, FastEnum.IsFlags, and FastEnum.IsEmpty to determine properties of an enumeration type. These methods help in understanding enum behavior and structure. ```csharp using FastEnumUtility; enum Sequential { A = 0, B = 1, C = 2, D = 3 } enum NonSequential { X = 1, Y = 5, Z = 10 } [Flags] enum Permissions { None = 0, Read = 1, Write = 2, Execute = 4 } enum Empty { } // Check if values are continuous Console.WriteLine($"Sequential is continuous: {FastEnum.IsContinuous()}"); // Output: Sequential is continuous: True Console.WriteLine($"NonSequential is continuous: {FastEnum.IsContinuous()}"); // Output: NonSequential is continuous: False // Check if FlagsAttribute is defined Console.WriteLine($"Permissions has Flags: {FastEnum.IsFlags()}"); // Output: Permissions has Flags: True // Check if enum is empty Console.WriteLine($"Empty is empty: {FastEnum.IsEmpty()}"); // Output: Empty is empty: True ``` -------------------------------- ### Numeric Conversions - ToInt32, ToInt64, etc. Source: https://context7.com/xin9le/fastenum/llms.txt Provides type-safe extension methods for converting enum values to their underlying numeric types (e.g., `ToByte`, `ToInt32`, `ToInt64`). Attempting to convert to a numeric type that does not match the enum's underlying type will result in an `ArgumentException`. ```csharp using FastEnumUtility; enum ByteEnum : byte { A = 1, B = 2 } enum IntEnum : int { X = 100, Y = 200 } enum LongEnum : long { P = 1000000000L, Q = 2000000000L } // Convert to specific numeric types byte byteVal = ByteEnum.A.ToByte(); Console.WriteLine($"Byte value: {byteVal}"); // Output: Byte value: 1 int intVal = IntEnum.Y.ToInt32(); Console.WriteLine($"Int32 value: {intVal}"); // Output: Int32 value: 200 long longVal = LongEnum.P.ToInt64(); Console.WriteLine($"Int64 value: {longVal}"); // Output: Int64 value: 1000000000 // Type mismatch throws ArgumentException try { int wrong = ByteEnum.A.ToInt32(); // ByteEnum has byte underlying type } catch (ArgumentException ex) { Console.WriteLine($"Type mismatch error caught"); } ``` -------------------------------- ### Parsing Comma-Separated Strings with System.Enum Source: https://github.com/xin9le/fastenum/blob/main/README.md Demonstrates the built-in capability of System.Enum.Parse to handle comma-separated flag strings, which is intentionally omitted in FastEnum for performance. ```cs //--- Assuming there is an enum type like following... [Flags] enum Fruits { Apple = 1, Lemon = 2, Melon = 4, Banana = 8, } //--- Passes comma-separated string var value = Enum.Parse("Apple, Melon"); Console.WriteLine((int)value); // 5 ``` -------------------------------- ### High-Performance Enum ToString with FastEnum Source: https://context7.com/xin9le/fastenum/llms.txt Use the FastToString extension method for significantly faster conversion of enum values to their string representations compared to the standard ToString() method. Handles undefined values by returning their numeric string. ```csharp using FastEnumUtility; enum Priority { Low = 1, Medium = 2, High = 3, Critical = 4 } var priority = Priority.High; // Fast string conversion (extension method) string result = priority.FastToString(); Console.WriteLine($"Priority: {result}"); // Output: Priority: High // Undefined values return numeric string var undefined = (Priority)99; Console.WriteLine($"Undefined: {undefined.FastToString()}"); // Output: Undefined: 99 ``` -------------------------------- ### Source Code Generation for Enums Source: https://github.com/xin9le/fastenum/blob/main/README.md Utilizes source code generation with FastEnum by annotating a target enum type. This approach offers the highest performance for critical scenarios. ```cs [FastEnum] // Annotate target enum type partial class HttpStatusCodeBooster // Placeholder for source code generation { } var x1 = FastEnum.ToString(HttpStatusCode.OK); var x2 = FastEnum.IsDefined(HttpStatusCode.OK); var x3 = FastEnum.Parse("OK"); var x4 = FastEnum.TryParse("OK", out var value); ``` -------------------------------- ### GetLabel - Multiple Label Annotations Source: https://context7.com/xin9le/fastenum/llms.txt Retrieves custom label values from an enum field using the LabelAttribute. Supports multiple labels per field by specifying an index. Use `GetLabel()` for the default label (index 0) or `GetLabel(index)` for specific labels. Set `throwIfNotFound` to `false` for safe retrieval of potentially missing labels. ```csharp using FastEnumUtility; enum Stock { [Label("Apple, Inc.")] [Label("AAPL", 1)] [Label("Technology", 2)] Apple = 0, [Label("Microsoft Corporation")] [Label("MSFT", 1)] [Label("Technology", 2)] Microsoft = 1 } // Get default label (index 0) string? companyName = Stock.Apple.GetLabel(); Console.WriteLine($"Company: {companyName}"); // Output: Company: Apple, Inc. // Get ticker symbol (index 1) string? ticker = Stock.Apple.GetLabel(index: 1); Console.WriteLine($"Ticker: {ticker}"); // Output: Ticker: AAPL // Get sector (index 2) string? sector = Stock.Microsoft.GetLabel(index: 2); Console.WriteLine($"Sector: {sector}"); // Output: Sector: Technology // Safe retrieval with throwIfNotFound = false string? missing = Stock.Apple.GetLabel(index: 99, throwIfNotFound: false); Console.WriteLine($"Missing label: {missing ?? "null"}"); // Output: Missing label: null ``` -------------------------------- ### IsContinuous / IsFlags / IsEmpty - Enum Properties Source: https://context7.com/xin9le/fastenum/llms.txt Utility methods to check properties of an enumeration type. ```APIDOC ## IsContinuous / IsFlags / IsEmpty ### Description Utility methods to check properties of an enumeration type such as continuity, FlagsAttribute presence, or emptiness. ### Response #### Success Response (200) - **result** (bool) - Boolean result based on the specific check performed. ``` -------------------------------- ### IsDefined - Check if Value is Defined Source: https://context7.com/xin9le/fastenum/llms.txt Checks if a constant with the specified value or name exists in the enumeration. ```APIDOC ## IsDefined ### Description Returns true if a constant with the specified value or name exists in the enumeration. ### Parameters #### Request Body - **value/name** (T/string) - Required - The value or name to check against the enum type. ### Response #### Success Response (200) - **result** (bool) - Returns true if defined, false otherwise. ``` -------------------------------- ### FastToString Source: https://context7.com/xin9le/fastenum/llms.txt Converts an enum value to its string representation with high performance. ```APIDOC ## FastToString ### Description Converts an enum value to its string representation with significantly better performance than the standard ToString() method. Undefined values return the numeric string. ### Method Extension Method ### Parameters - **value** (Enum) - Required - The enum value to convert to a string. ### Response - **string** - The string representation of the enum value. ``` -------------------------------- ### Parse String to Enum with FastEnum Source: https://context7.com/xin9le/fastenum/llms.txt Convert a string representation of an enum name or numeric value to its corresponding enum value. This method throws an ArgumentException if parsing fails and is case-sensitive by default, but can ignore case. ```csharp using FastEnumUtility; enum Color { Red = 1, Green = 2, Blue = 3 } // Parse by name (case-sensitive by default) var color1 = FastEnum.Parse("Red"); Console.WriteLine($"Parsed: {color1}"); // Output: Parsed: Red // Parse with case-insensitive option var color2 = FastEnum.Parse("blue", ignoreCase: true); Console.WriteLine($"Case-insensitive: {color2}"); // Output: Case-insensitive: Blue // Parse by numeric value var color3 = FastEnum.Parse("2"); Console.WriteLine($"Numeric: {color3}"); // Output: Numeric: Green // Attempting to parse invalid value throws exception try { var invalid = FastEnum.Parse("Yellow"); } catch (ArgumentException ex) { Console.WriteLine($"Error: {ex.Message}"); } ``` -------------------------------- ### FastEnum.TryParse Source: https://context7.com/xin9le/fastenum/llms.txt Attempts to parse a string to an enum value without throwing exceptions. ```APIDOC ## FastEnum.TryParse ### Description Attempts to parse a string to an enum value without throwing exceptions, returning a boolean indicating success. ### Method Static Method ### Parameters - **value** (string) - Required - The string to parse. - **ignoreCase** (bool) - Optional - Whether to perform case-insensitive parsing. - **result** (out T) - Required - The parsed enum value if successful. ### Response - **bool** - True if parsing succeeded, otherwise false. ``` -------------------------------- ### FastEnum.GetNames Source: https://context7.com/xin9le/fastenum/llms.txt Retrieves an immutable array of all names defined in the specified enumeration type. ```APIDOC ## FastEnum.GetNames ### Description Returns an immutable array of all names defined in the specified enumeration type. ### Method Static Method ### Parameters - **T** (Enum) - Required - The enumeration type to retrieve names from. ### Response - **ImmutableArray** - An array containing all defined enum names. ``` -------------------------------- ### Safe Enum Parsing with FastEnum TryParse Source: https://context7.com/xin9le/fastenum/llms.txt Safely attempt to parse a string to an enum value without exceptions. Returns a boolean indicating success or failure, useful for handling potentially invalid input gracefully. ```csharp using FastEnumUtility; enum LogLevel { Debug, Info, Warning, Error } // Successful parsing if (FastEnum.TryParse("Warning", out var level)) { Console.WriteLine($"Parsed level: {level}"); } // Output: Parsed level: Warning // Case-insensitive parsing if (FastEnum.TryParse("error", ignoreCase: true, out var errorLevel)) { Console.WriteLine($"Case-insensitive: {errorLevel}"); } // Output: Case-insensitive: Error // Failed parsing returns false if (!FastEnum.TryParse("InvalidLevel", out var result)) { Console.WriteLine("Parse failed, result is default value"); } // Output: Parse failed, result is default value ``` -------------------------------- ### FastEnum.GetName Source: https://context7.com/xin9le/fastenum/llms.txt Retrieves the name of the constant for the specified enumeration value. ```APIDOC ## FastEnum.GetName ### Description Retrieves the name of the constant for the specified enumeration value, returning null if the value is not defined. ### Method Static Method ### Parameters - **value** (Enum) - Required - The enum value to look up. ### Response - **string?** - The name of the enum constant, or null if undefined. ``` -------------------------------- ### Adding Multiple Label Annotations to Enum Fields Source: https://github.com/xin9le/fastenum/blob/main/README.md Applies multiple custom Label attributes to a single enum field, providing alternative labels or aliases. Access specific labels using an optional index. ```cs enum Company { [Label("Apple, Inc.")] [Label("AAPL", 1)] Apple = 0, } var x1 = Company.Apple.GetLabel(); // Apple, Inc. var x2 = Company.Apple.GetLabel(1); // AAPL ``` -------------------------------- ### FastEnum.GetValues Source: https://context7.com/xin9le/fastenum/llms.txt Retrieves an immutable array of all values defined in the specified enumeration type. ```APIDOC ## FastEnum.GetValues ### Description Returns an immutable array of all values defined in the specified enumeration type with zero allocation after initial caching. ### Method Static Method ### Parameters - **T** (Enum) - Required - The enumeration type to retrieve values from. ### Response - **ImmutableArray** - An array containing all defined enum values. ``` -------------------------------- ### Convert Enum Value to Member Object Source: https://context7.com/xin9le/fastenum/llms.txt Use FastEnum.ToMember to convert an enum value to its Member representation. This method supports deconstruction for easy access to the name and value. ```csharp using FastEnumUtility; enum FileAccess { Read = 1, Write = 2, ReadWrite = 3 } // Get member from value var member = FileAccess.ReadWrite.ToMember(); if (member != null) { Console.WriteLine($"Name: {member.Name}"); Console.WriteLine($"Value: {member.Value}"); Console.WriteLine($"Field: {member.FieldInfo.Name}"); // Deconstruction support var (name, value) = member; Console.WriteLine($"Deconstructed: {name} = {value}"); } // Output: // Name: ReadWrite // Value: ReadWrite // Field: ReadWrite // Deconstructed: ReadWrite = ReadWrite ``` -------------------------------- ### FastEnum.Parse Source: https://context7.com/xin9le/fastenum/llms.txt Converts a string representation of an enum name or numeric value to the equivalent enum value. ```APIDOC ## FastEnum.Parse ### Description Converts a string representation of an enum name or numeric value to the equivalent enum value. Throws ArgumentException if parsing fails. ### Method Static Method ### Parameters - **value** (string) - Required - The string to parse. - **ignoreCase** (bool) - Optional - Whether to perform case-insensitive parsing. ### Response - **T** - The parsed enum value. ``` -------------------------------- ### Check if Enum Value is Defined Source: https://context7.com/xin9le/fastenum/llms.txt Use FastEnum.IsDefined to check if a given value or name exists within an enumeration. Supports checking by value, undefined values, and by name using ReadOnlySpan. The extension method syntax is also available. ```csharp using FastEnumUtility; enum Permission { Read = 1, Write = 2, Execute = 4 } // Check by value bool isDefined = FastEnum.IsDefined(Permission.Read); Console.WriteLine($"Read is defined: {isDefined}"); // Output: Read is defined: True // Check undefined value bool isUndefined = FastEnum.IsDefined((Permission)99); Console.WriteLine($"99 is defined: {isUndefined}"); // Output: 99 is defined: False // Check by name (using ReadOnlySpan) bool hasRead = FastEnum.IsDefined("Read"); Console.WriteLine($"'Read' name exists: {hasRead}"); // Output: 'Read' name exists: True // Extension method syntax bool definedExt = Permission.Write.IsDefined(); Console.WriteLine($"Write is defined (extension): {definedExt}"); // Output: Write is defined (extension): True ``` -------------------------------- ### ToMember - Convert Value to Member Source: https://context7.com/xin9le/fastenum/llms.txt Converts an enum value to its Member representation. ```APIDOC ## ToMember ### Description Converts an enum value to its Member representation with support for deconstruction. ### Response #### Success Response (200) - **member** (Member) - The member object containing Name, Value, and FieldInfo. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.