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

How Can I Efficiently Represent Strings in C# Enums?

Linda Hamilton
Release: 2025-01-29 07:45:12
Original
245 people have browsed it

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:

[StringValue("FORMS")]
public enum AuthenticationMethod
{
    FORMS = 1,
    WINDOWSAUTHENTICATION = 2,
    SINGLESIGNON = 3
}

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

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

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;
    }
}
Copy after login
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:

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();
}
Copy after login
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 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