To create a C # index indicator

Source: Internet
Author: User
Tags foreach create index reset

During the process of program development, it is often necessary to access a set of objects, usually by creating an array list that is accessed by manipulating an array. The index indicator provided by C # allows us to easily and efficiently access a set of objects. Typically, we create a container class to store objects, and provides the corresponding operation method by implementing the enumerator interface. The following sample program demonstrates how to create and use an index indicator.

First step: Create a container class

In this code, ArrayList is used to enable us to manage objects using ArrayList features, and to implement IEnumerator interfaces, provide methods such as Movenext,reset, and enable the container class to support foreach operations.

class Employees:ienumerator//To enable container support (FOREACH ...). In .....   operation, you must implement the IEnumerator interface)
{
Private ArrayList m_employees;
  Defines a ArrayList object
private
int m_maxemployees;
Defines the maximum number of objects that a container can accept
//constructor, create a ArrayList object, and define the maximum number of objects that can be accepted
public Employees (int maxemployees)
{
M_m Axemployees = maxemployees;
M_employees = new ArrayList (maxemployees);       
}
//CREATE INDEX indicator by index ID
Public Employee
this[
Int index]
{
Get
{
       if (Index < 0 | | | index > M_EMPLOYEES.COUNT-1)
{
return null;

Return (Employee) M_employees[index];
}
Set
{
if (Index <0 | | index > M_MAXEMPLOYEES-1)
{
Retu RN;     
}
M_employees.insert (index,value);

}
//Custom Index indicator
Public Employee
this[
String SSN]
{
Get
{
Emp LoyEE empreturned = null;
foreach (Employee employee in m_employees)
{
if (employee).            SSN = = SSN)
{
empreturned = employee;
Break     
}
}
return empreturned

}
//provide the number of objects in the container
public
int Length
{
Get
{
return m_employees.count ;
}

//Implementation IEnumerator interface
Public IEnumerator GetEnumerator ()
{
Return m_employees.ge Tenumerator ();
}

public bool MoveNext ()
{
return m_employees.getenumerator (). MoveNext ();
}

public void Reset ()
{
M_employees.getenumerator (). Reset ();
}

Public object current
{
Get
{
return m_employees.getenumerator (). Current;
}
}
}

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.