Home > Backend Development > C++ > How to Efficiently Convert Strings to Enums in C#?

How to Efficiently Convert Strings to Enums in C#?

Patricia Arquette
Release: 2025-02-02 00:36:17
Original
534 people have browsed it

How to Efficiently Convert Strings to Enums in C#?

The best practice of the string to the enumeration conversion in the enumeration

In C# development, converting string to enumeration value is a common task. This article will explore the most effective way to achieve this conversion.

Found TryParse method

Starting from .NET CORE and .NET Framework ≥4.0, you can use the genealist method to resolve enumeration:

This method not only tries to resolve the string as enumeration value, but also converts it to an explicit enumeration type, and assigns it to MyStatus variable.

<code class="language-csharp">Enum.TryParse("Active", out StatusEnum myStatus);</code>
Copy after login
PARSEEEXTENSION/ToenumextExSion expansion method

For the older version of the .NET Framework, the custom expansion method can simplify the analysis process:

How to use:

or, you can use the toenum extension method:
<code class="language-csharp">public static T ParseEnum<T>(string value)
{
    return (T)Enum.Parse(typeof(T), value, true);
}</code>
Copy after login

Processing the default value
<code class="language-csharp">StatusEnum MyStatus = EnumUtil.ParseEnum<StatusEnum>("Active");</code>
Copy after login

In order to specify the default enumeration value when the conversion fails, you can use the expansion method with the default parameter:
<code class="language-csharp">public static T ToEnum<T>(this string value)
{
    return (T)Enum.Parse(typeof(T), value, true);
}

StatusEnum MyStatus = "Active".ToEnum<StatusEnum>();</code>
Copy after login

How to use:

Precautions

<code class="language-csharp">public static T ToEnum<T>(this string value, T defaultValue)
{
    if (string.IsNullOrEmpty(value))
    {
        return defaultValue;
    }

    T result;
    return Enum.TryParse<T>(value, true, out result) ? result : defaultValue;
}</code>
Copy after login
Although the expansion of the string clause class like Toenum looks convenient, it is important to maintain the integrity of the core category and avoid unnecessary ways to use other extensions or in contextual demand conflict. Come to confuse them. Therefore, it is recommended to create a dedicated class or name space for the custom enum conversion method.

The above is the detailed content of How to Efficiently Convert Strings to Enums in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template