This is the most complete article after reading several articles

Reprinted address : Enumeration features FlagsAttribute Usage of

Let's see the official explanation first : Indicates that enumeration can be used as a bit field ( It's a set of signs ) Handle .

It doesn't seem easy to understand , What exactly is processing as a bit field ?

In fact, the popular point is to use the binary representation to deal with the operation method of or and not in the concept of mathematical set .

What's the usage?

All business logic that can be abstracted into a mathematical set model can be handled with this method .

Take a chestnut :

In the design IO System time , We need to use a data structure to represent IO The operation authority of the system .

Respectively Read permissions , Write permission , Modify the permissions , Executive authority .

When adding permissions to a user, you need to assign a permission value to the user , But in the use of IO Function, you need to determine whether the user's permission value contains the permission of a function .

This business scenario is very suitable for data collection model .

Each permission is represented by a set .

The permissions assigned to users are represented by a total set of all allowed permissions .

When using functions , Whether the set of user assigned permissions contains the set of function permissions is handled by the algorithm .

In binary form

Or the example above , They are read permissions , Write permission , Modify the permissions , There are four permissions .

We use it 4 Bit length is represented by a binary number :

The first 0 Bit indicates read ;

The first 1 Bit indicates write ;

The first 2 Bit indicates modification ;

The first 3 Bit means execute ;

And the numbers in each position 0 Does not include ,1 Means to contain , namely 0 Indicates that there is no such permission ,1 Indicates that you have the permission .

So the binary numbers corresponding to each permission are :

Read :0001;

write in :0010;

modify :0100;

perform :1000;

When assigning permissions to a user , For example, the permissions to be assigned are read , write in , modify 3 Permissions .

We can use binary or operations to represent the union of sets .

0001 | 0010 | 0100 = 0111;

The user's permissions can be binary 0111 To express .

When using the function of , For example, I want to read a file , You need to determine whether the read function is included .

We can use binary sum operation to find out whether the intersection of user permission and read permission is equal to read permission , To indicate whether a set contains .

0111 & 0001 = 0001;

result 0001 And read permissions 0001 equal , So the user can use the read function .

use C# How to implement the code

Permission definition :

1
2
3
4
5
6
7
8
[Flags]
public  enum  IoPermission
{
     Read = 1,    // Read permissions :0001 The decimal value of is 1
     Write = 2,   // Write permission :0010 The decimal value of is 2
     Edit = 4,    // Modify the permissions :0100 The decimal value of is 4
     Run = 8,     // Executive authority :1000 The decimal value of is 8
}

Set permissions for users :

1
var  userPermission = IoPermission.Read | IoPermission.Write | IoPermission.Edit;

Judge whether the function is available :

1
var  readEnabled = (userPermission & IoPermission.Read) == IoPermission.Read;

in addition , The definition of permission can also be expressed by displacement element calculation :

1
2
3
4
5
6
7
8
[Flags]
public  enum  IoPermission
{
     Read = 1 << 0,    // Read permissions :1 Shift to the left in binary notation 0 position , namely 0001
     Write = 1 << 1,   // Write permission :1 Shift to the left in binary notation 1 position , namely 0010
     Edit = 1 << 2,    // Modify the permissions :1 Shift to the left in binary notation 2 position , namely 0100
     Run = 1 << 3,     // Executive authority :1 Shift to the left in binary notation 3 position , namely 1000
}

More calculations , And more application scenarios , Readers can do their own research on .

( Reprint )C# enumeration FlagsAttribute More about usage

  1. C++ Enumeration type Enum And C++11 Strong enumeration type usage

    C++ Enumeration types in are often the same as switch In combination with , Here's a simple switch Control the keyboard callback code fragment to illustrate the use of enumeration : //W A S D front . after . Left . Right walk enum Keydown{ Forward= ...

  2. 【 Reprint 】ssh-keygen Basic usage

    [ Reprint ]ssh-keygen Basic usage Original address :https://www.liaohuqiu.net/cn/posts/ssh-keygen-abc/ ssh Public key authentication is ssh One of the ways of Authentication . Through the public key ...

  3. 【 Reprint 】 mysql explain usage

    Reprint link :  mysql explain usage Website shows :     http://dev.mysql.com/doc/refman/5.7/en/explain-output.html Parameters :  htt ...

  4. Java enum( enumeration ) A detailed explanation of the use of ( turn )

    Use a : Constant stay JDK1.5 Before , We define constants as : public static fianl.... . Ok now , With enumeration , You can group related constants into an enumeration type , And enumerations provide more methods than constants . p ...

  5. [ Reprint ] extern &quot;C&quot; The usage of

    Reprinted from : http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html   1. introduction C++ The original intention of language is “a better ...

  6. C# Enumeration basic usage and extension method

    There's nothing to say , It's all the basics ! The code is as follows : using System; using System.Collections.Generic; using System.ComponentModel; usi ...

  7. Java ENUM The use of enumeration

    DK1.5 Introduced a new type —— enumeration . stay Java Although it is a “ Small ” function , But it brought me “ Big ” convenient . Use a : Constant stay JDK1.5 Before , We define constants as : publicstaticfianl... ...

  8. java Enumerate common usage

    Use a : Constant stay JDK1.5  Before , We define constants as : public static fianl.... . Ok now , With enumeration , You can group related constants into an enumeration type , And enumerations provide more methods than constants . p ...

  9. Java Enum The use of enumeration ( turn )

    explain :Java Enumeration ratio of dotnet The enumeration is easy to use , At least there are many ways to support it . Use a : Constant stay JDK1.5 Before , We define constants as : public static fianl.... . Ok now , With enumeration , Sure ...

Random recommendation

  1. Oracle Medium NVL function

    Oracle String processing previously described in functions in , Date function , Mathematical functions , And transfer functions and so on , Another kind of function is general function . There are mainly :NVL,NVL2,NULLIF,COALESCE, These functions can be used in all types . Next, Jane ...

  2. ARM-Linux S5PV210 UART drive (4)---- Serial driver initialization process

    about S5PV210 UART In terms of driving , The main concern is drivers/serial Under the samsung.c and s5pv210.c Even a file . from drivers/serial/Kconfig: config S ...

  3. Java FutureTask understand

    Respect the original author , Reprint please indicate the source : http://blog.csdn.net/gemmem/article/details/8956703 FutureTask To make up for Thread Designed for lack of , It can ...

  4. java fingerprint identification + Google Image Recognition Technology

    http://www.icodeguru.com/3/2451.html http://valseonline.org/thread-124-1-1.html

  5. centos 6.6 ios Image file download The official website and Alibaba cloud teach you to download

    1 use Baidu Search :centos Open the official website . Choose this one CET CENTOS 2 choice DVD ISO, Double click download Directly select left click to download We need thunder here Method Two open  https://mirrors.aliy ...

  6. Flex/AS3 base64 Specify character encoding

    public static function base64Encode(str:String, charset:String = "GBK"):String{ if(StringU ...

  7. first Polymer application - (2) Create your own elements

    Link to the original text : Step 2: Your own element Translation date : 2014 year 7 month 6 Japanese translators : Anchor through the last section of learning and practice , You have completed a basic application structure (application stru ...

  8. Linux Next use pv Monitoring progress

    Use pv Moving files pv example.mkv > /tmp/example.mkv Use pv monitor dd pv -cN source < example.iso | dd of=/dev/s ...

  9. hibernate When deleting a table in, the primary key is referenced by a foreign key, which violates the integrity constraints and cannot be deleted

    MySQL stay InnoDB Set up in foreign key relation , Cause unable to update or delete data . Can be set by FOREIGN_KEY_CHECKS Variables to avoid this .  SET FOREIGN_KEY_CHECKS ...

  10. [ WeChat development ] WeChat web page authorization Java Realization

    function : It is mainly used for users to access the third party through wechat on the mobile terminal H5 Get the user's identity information when the page is opened (openId, nickname , Head portrait , Location, etc ..) It can be used for wechat login . Wechat account binding . User authentication and other functions .     Preparation before development : 1. Need to be ...