15th Zhang Yi type and bit flags

Source: Internet
Author: User

1. Overview

This chapter contains enumeration types, bit flags, and methods for enumeration types.

2. Main content

  2.1 Enum types

An enumeration type defines a set of "symbol name/value" pairs.

The benefits of enumeration types include: ① can make programs easier to write, read, and maintain. The ② enumeration type is strongly typed and has compiler detection.

When compiling enumeration types, the C # compiler converts each symbol into a constant field of type.

The System.Enum type has a static method named Getunderlyingtype that returns the underlying type that holds the value of an enumerated type.

Enum.getunderlyingtype (typeof(Color));

  2.2-bit flag

An enumeration type represents a single numeric value, while a bit flag represents a set of bits, some of which are in the on state, and some bits are in the off state.

Using the [Flags] attribute, you can ensure that the correct enumeration value is returned after the bitwise operation.

Never use the IsDefined method on a bit flag enumeration type.

  2.3 Adding a method to an enumeration type

You can use the extension method functionality of C # to simulate adding methods to enum types.

3. Summary

  This chapter describes some of the less common features of enumeration types.

Reasonable use of bit marks can greatly improve efficiency.

  

15th Zhang Yi type and bit flags

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.