Home >Backend Development >C++ >How Can I Efficiently Represent Strings in C# Enums?

How Can I Efficiently Represent Strings in C# Enums?

Linda Hamilton
Linda HamiltonOriginal
2025-01-29 07:45:12282browse

How Can I Efficiently Represent Strings in C# Enums?

C# enumerated string indicates

In C#, enumeration usually uses a value to represent different options. However, in some cases, you may be more inclined to associate with meaningful string with these options.

One method is to use custom attributes and string search methods:

<code class="language-csharp">[StringValue("FORMS")]
public enum AuthenticationMethod
{
    FORMS = 1,
    WINDOWSAUTHENTICATION = 2,
    SINGLESIGNON = 3
}

public static class StringEnum
{
    public static string GetStringValue(Enum value)
    {
        // 检索 StringValue 属性,如果找到则返回关联的字符串;否则返回 null。
    }
}</code>
This method requires manual application attributes and is considered to be relatively long.

Another method is to consider the type of safe lift mode:

<code class="language-csharp">public sealed class AuthenticationMethod
{
    private readonly string name;
    private readonly int value;

    public static readonly AuthenticationMethod FORMS = new AuthenticationMethod(1, "FORMS");
    public static readonly AuthenticationMethod WINDOWSAUTHENTICATION = new AuthenticationMethod(2, "WINDOWS");
    public static readonly AuthenticationMethod SINGLESIGNON = new AuthenticationMethod(3, "SSN");

    private AuthenticationMethod(int value, string name)
    {
        this.name = name;
        this.value = value;
    }

    public override string ToString()
    {
        return name;
    }
}</code>
This model involves creating a seal similar to the lift, where the static reading field represents each option. Each field has a string representation form and value.

For the conversion of the type of explicit type, consider adding the type conversion operator defined by the mapping dictionary and the user definition:

<code class="language-csharp">private static readonly Dictionary<string, AuthenticationMethod> instance = new Dictionary<string, AuthenticationMethod>();

public AuthenticationMethod(int value, string name)
{
    instance[name] = this;
}

public static explicit operator AuthenticationMethod(string str)
{
    if (instance.TryGetValue(str, out AuthenticationMethod result))
        return result;
    else
        throw new InvalidCastException();
}</code>
This method combines the advantages of the two methods: the type of safe and convenient string representation and the type conversion of the user definition.

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

Statement:
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