Hello everyone, I am SUNWEN. I learned this afternoon that I am going to be a "gender" in the evening ". well, our school is very strange. We need to take turns to patrol all night, from PM to AM. I have a friend from the physics department. I found a billboard in front of Tianjia binglou last time. I slept below and laughed at me! Haha!
Now I want to talk about the container in C. this is a very important topic, because no matter what program you write, you can't help dealing with containers. what is a container? (inverted !). A container is something that can hold things !), In the object-oriented programming languages C # and JAVA, containers are called objects that can accommodate objects. Doesn't it mean that "Everything is an object? "In the past, a programmer who worked on C ++ told me that containers in JAVA are much better than C ++. C #, a later developer of JAVA, has no doubt that its container function is also very powerful.
The foreach statement is the simplest way to traverse the elements of a container. we can use System. collections. IEnumerator class and System. collections. the IEnumerable interface is used to use the container in C #. In the following example, the function is a string splitter.
000: // CollectionClassesokens. cs
001: using System;
002: using System. Collections;
003:
004: public class Tokens: IEnumerable
005 :{
006: private string [] elements;
007:
008: Tokens (string source, char [] delimiters)
009 :{
010: elements = source. Split (delimiters );
011 :}
012:
013: // reference IEnumerable interface 014:
015: public IEnumerator GetEnumerator ()
016 :{
017: return new TokenEnumerator (this );
018 :}
019:
020:
021:
022: private class TokenEnumerator: IEnumerator
023 :{
024: private int position =-1;
025: private Tokens t;
026:
027: public TokenEnumerator (Tokens t)
028 :{
029: this. t = t;
030 :}
031:
032: public bool MoveNext ()
033 :{
034: if (position <t. elements. Length-1)
035 :{
036: position ++;
037: return true;
038 :}
039: else
040 :{
041: return false;
042 :}
043 :}
044:
045: public void Reset ()
046 :{
047: position =-1;
048 :}
049:
050: public object Current
051 :{
052: get
053 :{
054: return t. elements [position];
055 :}
056 :}
057 :}
058:
059: // test 060:
061: static void Main ()
062 :{
063: Tokens f = new Tokens ("This is a well-known-done program.", new char [] {,-});
064: foreach (string item in f)
065 :{
066: Console. WriteLine (item );
067 :}
068 :}
069 :}
The output in this example is:
This
Is
A
Well
Done
Program.
Well, this section is about it. The environment is not very good now. A large group of students are watching VCD.