Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C # enumerated types

Shulou Source: shulou.com Published: 2022-06-02 18:38:53 11月13日 Update

This article mainly shows you "how to use C# enumeration types". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use C# enumeration types".

What do you need to know when using C # enumerated types? First of all, let's look at an example:

Public enum Colors {Red = 1, Green = 2, Blue = 4, Yellow = 8}; The entries of the Colors Enum are: Red Green Blue Yellow

A summary of the use of C # enumerated types gets the type of Enum based on name:

Colors mycolor = (Colors) Enum.Parse (typeof (Colors), "red", true); (int) mycolor1=1 mycolor1.GetTypeCode=Int32

A summary of the use of C # enumerated types gets the type of Enum based on value:

Colors mycolor = (Colors) Enum.Parse (typeof (Colors), "1", true); mycolor2.ToString () = Red mycolor2.GetTypeCode=Int32

The contents of a summary traversal enumeration used by the C # enumeration type

Foreach (string s in Enum.GetNames (typeof (Colors) {/ / to do} Colors myOrange = (Colors) Enum.Parse (typeof (Colors), "Red, Blue,Yellow"); The myOrange value has the combined entries of [myOrange.ToString ()] = 13 Colors myOrange2 = (Colors) Enum.Parse (typeof (Colors), "Red, Blue") The myOrange2 value has the combined entries of [myOrange2.ToString ()] = 5 is all the content of the article "how to use C# enumerated types". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Type content article learning help example aspect time easy to understand more organization knowledge editing industry information information channel channel research Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi Microsoft macOS Apple