How to Use enum and foreach

Source: Internet
Author: User
In a program to be written today, I want to use foreach and enum to implement a function and find that I have forgotten it. Now I want to sort it out, paste it to your friends to make it easy and prevent you from forgetting it!
I am still a student. If you have any ideas, please advise!

First, paste the code I wrote. (to highlight the key point, only write the code that can respond to your needs !)

Public enum tableNames
{
T_Que_marketCompare_algorithm = 0, // market comparison method
T_que_cost_approach_newLand // cost method, newly developed land
}

Foreach (string item in Enum. GetNames (typeof (tableNames )))

{
String usingTable = item;
}

Usage:
Enum

Enum is usually used when the program needs to indicate that it is used by default. The younger brother is used in the tables to be accessed. Because the tables to be used are fixed, the conditions are met!
Enumeration is a specified constant, which can be assigned a value or not. If it is not assigned a value, it starts from 0 by default, followed by 1. however, you can also obtain other attributes as needed. For example, you can obtain the name of the expected table, because
Enum. GetNames (typeof (tableNames) returns its name! Others are called based on your own needs !!!

Value assignment: tableNames myTable = tableNames. t_Que_marketCompare_algorithm;
Convert int I = Convert. ToInt32 (myTable); myTable = (tableNames) 2; // The usage is similar to a common type !!

Foreach
Foreach is usually used to access each element in the array and set. It is similar to for loop. You can use the break keyword to jump out of the loop or use the continue keyword to enter the next round of lower generation of the loop, it is worth noting that both its type and identifier are required, that is, string item; foreach (item in Enum. getNames (typeof (tableNames) {} returns an error. It must be written as foreach (string item in Enum. getNames (typeof (tableNames )))!!!!

By the way, to prevent forgetting
Put the struct array I wrote and its usage here,
Struct
Public struct usingcase
{
Public String groundid;
Public String caseid;
Public int year;
Public int season;
Public String strpurpose;
}

Struct array:
Public static usingcase [] casecollection = new usingcase [1000];
Usage:
Public usingCase myUsingCase = new usingCase ();
The value assignment is the same as the call by a common member, for example, myUsingCase. groundID = "wonderfu! ";
CaseCollection [number] = myUsingCase;
It's no different from what we usually use!

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.