C # advanced tutorial (10) C # containers

Source: Internet
Author: User
Tags string splitter

C # containers

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:File: // referenceUse 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.

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.