About enum in C #

Source: Internet
Author: User

Enum is an enumeration type. It is the same as struct, int, single, and double. It belongs to the value type and is stored in the stack. It does not need to allocate internal space when it is created, so it is good for program performance.

Why should we introduce enum? One reason is to make the program safer and more flexible.

What should I do? When we have determined the number of changes, we can enumerate the changes one by using enum.

For example, there is a chart that can be displayed in three types: line, bar, and pie. You can specify the display method as needed, in this case, we can use variables for control, or use more secure enumeration types for control.

Namespace Constants
{
Public class SystemConstant
{
Public Constants. Webinfo ChartTypes {get; set ;}
Public string Result;
Public string Create ()
{
ChartTypes = Constants. Webinfo. Pie;
Switch (ChartTypes)
{
Case Constants. Webinfo. Bar:
Result = "bar ";

// Code statement

//.......

//........
Break;
Case Constants. Webinfo. Line:
Result = "line ";
Break;
Case Constants. Webinfo. Pie:
Result = "pie ";
Break;
}
Return Result;
}
}
Public enum Webinfo
{
Pie, Bar, Line
}
}

See it. The red part is my enum. In the call, use Constants. webinfo. bar, Constants. webinfo. line, or Constants. webinfo. pie is called. The type is safer and no error characters are written.

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.