Container, Iteration

Source: Internet
Author: User
Array, set, ienumerable interface, iterator

Development: array --> set --> generic

(1) array
1. the array data structure is an instance of the system. array class.
2. the syntax of the system. array class is
[Serializableattribute]
[Comvisibleattribute (true)]
Public abstract class array: icloneable, ilist, icollection, ienumerable
3. Let's look at an example of using arrays (I call it implicit implementation)
Protected void page_load (Object sender, eventargs E)
{
String [] strarrname = new string [3] {"Jim", "Andy", "Sun "};
Foreach (string strname in strarrname)
{
Lblname. Text + = strname + ",";
}
}
Or write it like this.
Protected void page_load (Object sender, eventargs E)
{
String [] strarrname = new string [3];
Strarrname [0] = "Jim ";
Strarrname [1] = "Andy ";
Strarrname [2] = "sun ";
Foreach (string strname in strarrname)
{
Lblname. Text + = strname + ",";
}
}
The result is as follows:
Jim, Andy, sun,
4. Let's look at another example of using arrays (I call it explicit implementation)
Protected void page_load (Object sender, eventargs E)
{
Array myarray = array. createinstance (typeof (string), 3 );
Myarray. setvalue ("Jim", 0 );
Myarray. setvalue ("Andy", 1 );
Myarray. setvalue ("sun", 2 );
Foreach (string strname in myarray)
{
Lblname. Text + = strname + ",";
}
}
The result is as follows:
Jim, Andy, sun,
5. advantages: it can be accessed efficiently to specify the target element. The system. array class has its own C # syntax, which is very intuitive to program.
6. disadvantage: the array size must be specified during instantiation, and cannot be added, inserted, or deleted later.

(2) set
1. we use the collection data structure to address the disadvantages of the array data structure.
2. classes in the collection data structure are located in system. in the collections namespace.
3. when it comes to collections, we must first understand several interfaces. for details about the following interfaces, see (3) set interface
3.1 ienumerable interface and ienumerator interface
3.2 icollection interface
Public interface icollection: ienumerable
3.3 ilist interface
Public interface ilist: icollection, ienumerable
3.4 idictionary interface
Public interface idictionary: icollection, ienumerable
4. note:
4.1 The icollection interface is system. the base interface of the class in the collections namespace.
4.2 icollection interface extended ienumerable; idictionary and ilist are more dedicated interfaces for icollection extension. idictionary is a set of key/value pairs, such as the hashtable class. the ilist implementation is a set of values, and its members can be accessed through indexes, such as the arraylist class.
4.3 some collections (such as the queue and stack classes) restrict access to their elements. They directly implement the icollection interface.
4.4 if neither the idictionary interface nor the ilist interface meets the requirements of the required collection, a new collection class is derived from the icollection interface to improve flexibility.

(3) ienumerable interface and ienumerator Interface
1. My understanding: only the data structure class that implements the ienumerable interface can use the foreach statement. The following is an example.
// Person class
Public class person
{
Private string _ firstname;
Private string _ lastname;

Public String firstname
{
Get {return _ firstname ;}
}
Public String lastname
{
Get {return _ lastname ;}
}

Public Person (string strfirstname, string strlastname)
{< br> This. _ firstname = strfirstname;
This. _ lastname = strlastname;
}< BR >}< br> // set of personlist to implement the ienumerable interface
public class personlist: ienumerable
{< br> private person [] _ arrperson;

Public personlist (person [] myarrperson)
{< br> _ arrperson = new person [myarrperson. length];
for (INT I = 0; I {< br> _ arrperson [I] = myarrperson [I];
}< BR >}

Public ienumerator getenumerator ()
{
Return new peopleenumerator (_ arrperson );
}
}
// Implement the ienumerator Interface
Public class peopleenumerator: ienumerator
{
Private int _ Position =-1;
Public person [] _ arrperson;

Public object current
{< br> Get
{< br> try
{< br> return _ arrperson [_ Position];
}< br> catch (indexoutofrangeexception)
{< br> throw new invalidoperationexception ();
}< BR >}

Public peopleenumerator (person [] myarrperson)
{
_ Arrperson = myarrperson;
}

Public bool movenext ()
{
_ Position ++;
Return (_ Position <_ arrperson. Length );
}

Public void reset ()
{< br> _ Position =-1;
}< BR >}< br> // use of the set
protected void page_load (Object sender, eventargs e)
{< br> person [] myarrperson = new person [3]
{< br> new person ("John", "Smith "),
new person ("Jim", "Johnson"),
new person ("Sue", "rabon"),
};

Personlist mypersonlist = new personlist (myarrperson );
Foreach (person myperson in mypersonlist)
{
Lblname. Text + = myperson. firstname + "" + myperson. lastname + ",";
}
}
6. In my understanding, if a collection class is defined to implement the ienumerable interface, a class corresponding to the implementation of the ienumerator interface must be defined. Isn't this very troublesome?

(4) iterator
1. Use the iterator to avoid the above troubles and modifyCode, Pay attention to the orange part
Public class person
{
Private string _ firstname;
Private string _ lastname;

Public String firstname
{
Get {return _ firstname ;}
}
Public String lastname
{
Get {return _ lastname ;}
}

Public Person (string strfirstname, string strlastname)
{
This. _ firstname = strfirstname;
This. _ lastname = strlastname;
}
}

Public class personlist: ienumerable
{
Private person [] _ arrperson;

Public personlist (person [] myarrperson)
{< br> _ arrperson = new person [myarrperson. length];
for (INT I = 0; I {< br> _ arrperson [I] = myarrperson [I];
}< BR >}

Public ienumerator getenumerator ()
{
// When the compiler detects the iterator, it automatically generates the current, movenext, and dispose methods for the ienumerable or ienumerable <t> interfaces.
For (INT I = 0; I <_ arrperson. length; I ++)
{
Yield return _ arrperson [I];
}
}
}

Example
Classes that implement the ienumerable interface can perform simple iterations, such as foreach statements.

Using System;
Using System. Collections. Generic;
Using System. text;
Using System. collections;

Namespace Ienumerable Interface
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
A =   New A ();
Foreach ( Int ? VaR In A) // When the int value may be null?

{
Console. writeline (VAR );

}

}
}
Class A: ienumerable
{
Int X =   1 ;
Int Y =   2 ;
Int Z =   3 ;
Public Ienumerator getenumerator ()
{
// Int I =-1;
For ( Int I =   0 ; I <= 3 ; I ++ )
{


If (I = X)
{
YieldReturn 1;
}
Else   If (I = Y)
{
YieldReturn 2;

}
Else   If (I = Z)
{
YieldReturn 3;

}
Else
{
YieldReturn Null;
}
}
}
}
}

URL: http://www.cnblogs.com/illele/archive/2008/04/22/1166340.html

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.