The flags attribute is used in C # enumerations

Source: Internet
Author: User

. NET enumeration we generally have two usages, one is to represent the unique sequence of elements, and the other is to represent a variety of composite states. This time it is generally necessary to mark the enumeration with the [Flags] attribute as a bit field, so that we can use the "or" operator to combine multiple states.

For example:

[Flags]
public enum Permission
{
Create = 1,
Read = 2,
Update = 4,
Delete = 8,
}



Permission Permission = Permission.create | Permission.read | Permission.update | Permission.delete;
Console.WriteLine ("1, enum creation, and Assignment ...");
Console.WriteLine (permission. ToString ());
Console.WriteLine ((int) permission);

Permission = (permission) enum.parse (typeof (Permission), "5");
Console.WriteLine ("2, convert by numeric string ...");
Console.WriteLine (permission. ToString ());
Console.WriteLine ((int) permission);

Permission = (permission) enum.parse (typeof (Permission), "Update, delete, read", true);
Console.WriteLine ("3, converting by enumeration name string ...");
Console.WriteLine (permission. ToString ());
Console.WriteLine ((int) permission);

Permission = (permission) 7;
Console.WriteLine ("4, direct with digital coercion ...");
Console.WriteLine (permission. ToString ());
Console.WriteLine ((int) permission);

Permission = permission & ~permission.read;
Console.WriteLine ("5, remove an enumeration item ...");
Console.WriteLine (permission. ToString ());
Console.WriteLine ((int) permission);

Permission = permission| Permission.delete;
Console.WriteLine ("6, plus an enumeration ...");
Console.WriteLine (permission. ToString ());
Console.WriteLine ((int) permission);


In the database, Judge:
and (@permission is NULL or @permission =0 or permission & @permission [email protected])


Reference: http://www.cnblogs.com/youring2/archive/2011/12/16/2289832.html

The flags attribute is used in C # enumerations

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.