Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define the enumeration and bit enumeration of C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to define the enumeration and bit enumeration of C#. The content is detailed and easy to understand, the operation is simple and fast, and it has certain reference value. I believe you will gain something after reading this article on how to define enumeration and bit enumeration of C#. Let's take a look.

I. the concept of enumeration

C # enumeration (Enum), the enumerated type is the basic data type (value type) used to declare a set of named constants

II. Definition of enumerations

Declare the enum variable:

Enum {enumeration list}

Where enum_name specifies the type name of the enumeration, and enumeration list is a comma-separated list of identifiers; each symbol in the enumeration list represents an integer value, an integer value larger than the symbol preceding it. By default, the value of the first enumeration symbol is 0. For example:

Enum Days {Sun, Mon, tue, Fir, sat}; III. Enumerated cases, use of enumerated variables

Example

Using System;namespace EnumApplication {class EnumProgram {enum Days {Sun, Mon, tue, Wed, thu, Fri, sat}; static void Main (string [] args) {int WeekdayStart = (int) Days.Mon; int WeekdayEnd = (int) Days.Fir; Console.WriteLine ("Monday: {0}", WeekdayStart) Console.WriteLine ("Firday: {0}", WeekdayEnd); Console.ReadKey ();} IV. Enum class:

All enumerated types implicitly inherit System.Enum types, and System.Enum types are the only reference types that inherit from System.ValueType types that are not value types.

5. The Enum class method describes that CompareTo compares this instance with the specified object and returns an indication Equals of the relative values of the two: indicating whether the instance is equal to the specified object

Format converts the specified value of the specified enumeration type to its equivalent string representation GetName retrieves the name of the constant with the specified value in the specified enumeration GetNames retrieves the array GetTypeCode of the constant name in the specified enumeration returns the base TypeCodeGetUnderlyingTye of this instance the base type GetValues of the specified enumeration the array HasFlag of the constant value in the specified enumeration determines whether one or more bit fields are set in the current instance IsDefined returns an indication of whether there is a constant with the specified value in the specified enumeration Parse converts the name or number of one or more enumeration constants into an equivalent enumeration object A parameter specifies whether the operation is case-insensitive TryParse converts the name or number of one or more enumeration constants into an equivalent enumeration object, indicating whether the conversion successfully returns values 6, flag enumeration / bit enumeration

C # tag enumeration (Flags): enumerated types are used to declare a set of basic type data (value types) with named constants.

Enumerated values are mutually exclusive. A set of bit tags is a list of elements that appear in a combination, usually designed to combine new values with a "bit OR" operation.

Enumerated types usually express a set of values that are semantically independent. The most perfect combination is to implement the set of bit tags by enumerating types, which is referred to as bit enumeration.

7. Definition of bit enumeration / permission enumeration / [Flags] public enum permission Note after adding the [Flags] feature, there are three ways to write it: {one is to use the instance / / 1 of octet enumeration to create, read, modify and delete permissions for users var parmission = Permission.Create | parmission.Read | parmission.Update | parmission.Delete / / 2. Remove the user's permission to modify and delete parmission = parmission & ~ parmission.Update;parmission = parmission & ~ parmission.Delete;//3, add the permission to modify parmission = parmission | parmission.Update;//4, and determine whether the user has the permission to create var isCreate = (parmission & parmission.Create)! = 0ram / or var isCreate = (parmission & parmission.Create) = = parmission.Create At this point, the value of the parmission enumeration will become 0-1-4-5, and its ToSting () will become "parmission.Create, parmission.Read". Here we can explain why the fifth value, Delete, is 8 but not 5. In other words, its value should not be a compound value of the first few values. A relatively simple method is to use the n-th power of 2 to assign each item in turn, for example: 1, 2, 4, 8, 16, 16, 64. This is the end of the article on "how to define enumeration and bit enumeration of C#". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to define enumeration and bit enumeration of C#". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report