C # foreach loop instance details

Source: Internet
Author: User

When writing C # code, we will find that using a foreach loop is more convenient than using a for loop. You do not need to forcibly convert the data type, and you do not need to use a subscript! The help document shows that if you want to use foreach for an object, the object type must have the GetEnumerator method. This method is under the IEnumerable interface, but it doesn't matter whether this interface is implemented! The GetEnumerator method needs to return an instance of IEnumerator, because an attribute: Current and a method: MoveNext will be used during the foreach time! The source code with comments is as follows:
Using System;
Using System. Collections;

Namespace ConTest01
{
/// <Summary>
/// Summary of Class1.
/// </Summary>
Class Class1
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main (string [] args)
{
Persons p = new Persons ("jack", "tom", "marry", "mike ");
Foreach (string s in p)
{
Console. WriteLine (s );
}
}
}

Public class Persons
{
Public string [] m_Names;

Public Persons (params string [] Names)
{
This. m_Names = new string [Names. Length];
Names. CopyTo (this. m_Names, 0 );
}

Public string this [int index]
{
Get
{
Return this. m_Names [index];
}
Set
{
This. m_Names [index] = value;
}
}

# Region IEnumerable Member

// This method is required if the class needs to be traversed by foreach. It doesn't matter whether the IEnumerable interface is implemented.
Public IEnumerator GetEnumerator ()
{
// Returns an instance of IEnumerator.
Return new PersonsEnumerator (this );
}

# Endregion

}

Public class PersonsEnumerator: IEnumerator
{
Private int index =-1;
Private Persons P;

Public PersonsEnumerator (Persons P)
{
This. P = P;
}

# Region IEnumerator Member

// Reset method
Public void Reset ()
{
This. index =-1;
}

// Get the attribute of the current value, which is read-only. Therefore, the element content to be traversed cannot be modified during the foreach time.
Public object Current
{
Get
{
Return this. P [index];
}
}

// Move the indexes in the current instance to one place and determine whether there are any elements after the instance advances.
Public bool MoveNext ()
{
Int tempIndex = this. index;
If (tempIndex> = this. P. m_Names.Length)
{
Return false;
}
Else
{
Return true;
}
}
# Endregion

}

Add, modify, and delete objects. Nothing else. A problem occurs during deletion.
 
This is because an element is deleted from a collection of classes. In this way, we need to traverse the entire set, and foreach is a new tool for traversing. Naturally, it is used. The code is similar to the following:
 
String temp = name. Text; // read data from TextBox
Foreach (LCourse cou in Data. myCourse) // traverse in List
{
If (cou. name = temp) // judge whether the cou name matches
{
Data. myCourse. Remove (cou); // The matched items are to be deleted and removed from the list.
Break; // skip Loop
}
}

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.