Home > Backend Development > C++ > How Can We Improve String Representation of Enumerations Using Type-Safe Enums and Explicit Type Conversion?

How Can We Improve String Representation of Enumerations Using Type-Safe Enums and Explicit Type Conversion?

DDD
Release: 2025-01-29 07:46:08
Original
941 people have browsed it

How Can We Improve String Representation of Enumerations Using Type-Safe Enums and Explicit Type Conversion?

A alternative of the enumeration string representation

The previous scheme uses custom attributes to retrieve enumeration string representation. Although the function is valid, it may look lengthy. The following is an alternative method to use type secure enumeration mode:

This model defines the explicit instance of enumeration, which contains string and value representation.
<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>
Copy after login
Method return the string representation form.

ToString() Different type conversion

In order to enable the explicit type conversion, you can add a static member for mapping to the class:

Fill in the dictionary in the constructor of the class:

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

Finally, add a user -defined type conversion operator:

<code class="language-csharp">instance[name] = this;</code>
Copy after login

This allows you to explicitly convert the string to

instance to make the type conversion process more direct.
<code class="language-csharp">public static explicit operator AuthenticationMethod(string str)
{
    AuthenticationMethod result;
    if (instance.TryGetValue(str, out result))
        return result;
    else
        throw new InvalidCastException();
}</code>
Copy after login

The above is the detailed content of How Can We Improve String Representation of Enumerations Using Type-Safe Enums and Explicit Type Conversion?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template