C # (11) enumeration (Enums)

Source: Internet
Author: User

1.11 enumeration (Enums)

An enumeration Declaration defines a unified category name for constants with the same attributes. It is often used for constants with a known range during compilation. However, the specific values of these constants can be determined only during execution. For example, it is known that the primary colors are red, blue, and green, and they belong to the same color. It can be defined as follows:

*/
Enum Color {
Red,
Blue,
Green
}
/*

We create a shape class. Each shape has a color. Color is a property of "shape. However, the specific color can only be determined during execution:

*/
Class Shape
{
Public void Fill (Color color ){
Switch (color ){
Case Color. Red:
...
Break;
Case Color. Blue:
...
Break;
Case Color. Green:
...
Break;
Default:
Break;
}
}
}
/*

This File method illustrates how to assign a given color to the shape class. Enumeration is superior to ordinary Integer constants in that it makes the code easier to read, understand, and secure. The enumerated constants can be determined by the compiler. When used, the compiler can also check its validity. Enumeration is not actually unique to c. Hey, I will not introduce it in detail. Take the opportunity to take a shot! If anyone is interested, read your own books! (In order to avoid eating too much bananas, I will introduce the book "C programming FAQ" Tsinghua 1996. 29.00 RMB. Even though it is old, I can still see it in the bookstore)

Related Article

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.