Flag in the enum of C #

Source: Internet
Author: User

We know that by default, the first enumerator has a value of 0, followed by the value of each enumerator plus 1.

Enum days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

We can also use the initial value to override the default value.

Enum days {sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

This sequence of elements starts at 1 instead of 0.

Each enumeration type has an underlying type, which can be any shaping except char.

Enum Days:byte {sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

The enumeration types that are allowed are byte, sbyte, short, ushort, int, uint, long, or ulong.

We then talk about the middle of this section, declaring the FlagsAttribute attribute on the enum, which allows the enumeration to participate in the binary and/or operation.

byte, sbyte, short, ushort, int, uint, long, or ulong. " > Specifically, let's look at the code:

Sing unityengine;using system.collections;using System; [flags]public enum loglevel1{none = 0,debug = 1,info = 2,warning = 4,error = 8,except = 16,critical = 32,}public Class tes T:monobehaviour {public static LogLevel1 loglevel = Loglevel1.debug | Loglevel1.info | loglevel1.warning | Loglevel1.error | loglevel1.critical | loglevel1.except;//Use this for initializationvoid Start () {print (loglevel);//Print Loglevel1.debug | Loglevel1.info | loglevel1.warning | Loglevel1.error | loglevel1.critical | Loglevel1.except All enumerations
Print (LogLevel & loglevel1.debug);//Printing Loglevel1.debug Enumeration
if (Loglevel1.debug = = (LogLevel & loglevel1.debug)) {print (loglevel);//print all enumerated types
}}//update is called once per framevoid update () {}}

Here I am testing in unity, and you can see that the bitwise operator of the | and & can manipulate the enumeration type, and discover if (Loglevel1.debug = = (LogLevel & loglevel1.debug)) The function of this code is

Determine if the enum contains debug this enumeration

Flag in the enum of C #

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.