Enumerating the uses of an enum

Source: Internet
Author: User

First, the preface

It is well known to enumerate enums, but it is not possible to abstract a common solution when there are so many scenarios in the enumeration. Code everyone will write, but not everyone likes to write duplicate code, always with CTRL + C and CTRL + + tired AH? Many people like me, very dislike to write duplicate code, the code is written more, the bug is many. For common scenarios, most people like to abstract out, write a set of common, everywhere can be used, and not easy to make mistakes. Of course, you like CTRL + C and CTRL + V, I have no way ....

Two, int value, string value converted to enum

As follows, a simple enumeration:

Public enum ExchangeType
    {
        [Enumfieldattribute (Unlimited, true)] all 
        = 0,
        [Enumfieldattribute ("Shenzhen Stock Exchange") ]
        szse = 1
    }

For the following input code:

ExchangeType type = (ExchangeType) 3;
    
            if (type = = Exchangetype.all)
            {
                Console.WriteLine ("Exchangetype.all.");
            }
            else if (type = = Exchangetype.szse)
            {
                Console.WriteLine ("Exchangetype.szse.");
            }
            else
            {
                Console.WriteLine ("Non exist!");
            }

For a cast, let's guess what it will output ....

For the result, you can test it yourself, it may be inconsistent with the value you expect!

Also for the following code:

ExchangeType type;

BOOL success = Enum.tryparse<exchangetype> ("3", out type);

The return result performed by TryParse success is also successful (true), but the expected value is not necessarily correct.

Therefore, it is easy to make mistakes for enumeration conversions. Therefore, you must provide a default value to ensure that the correct value is returned when the conversion fails. The code for the int value and string value to be converted to an enum is as follows:

public static T toenum<t> (int value, T defaultt) where t:struct
        {
            string enumname = Enum.getname (typeof (T) , value);
    
            Return toenum<t> (Enumname, DEFAULTT);
        }
    
        public static T toenum<t> (string enumname, T defaultt) where t:struct
        {
            if (string). Isnullorwhitespace (enumname))
            {return
                defaultt;
            }
    
            T result;
    
            if (! Enum.tryparse<t> (Enumname.trim (), out result))
            {return
                defaultt;
            }
    
            if (enum.isdefined (typeof (T), result)
            }
    
            return DEFAULTT;
        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.