The----of the Sunwen Tutorial C # Advanced 10

Source: Internet
Author: User
Tags foreach
Tutorial Sunwen Tutorial----C # Advanced
(10)
Mrfat@china.com
Hi, I'm sunwen. I learned this afternoon, we're going to be "Watchman" at night. Oh, our school is very strange, we have to take turns to patrol overnight, from 10:30 to 6:00. I have a friend of physics department, last time in front of Tin Ka Ping building to find a propaganda board, Mat under a sleep, It's killing me!

Now what I want to say is the container in C #. This is a very important topic, because no matter what kind of program you write, you have to deal with the container. What is a container (pour!). A container is something that can hold something (and then pour!), in C # and Java, an object-oriented programming language, the container is called an object, not to say "everything is an object?" In the past, I have a C + + programmer friend told me that the Java container is too good to use, more useful than C + +. As a successor to Java, C # Undoubtedly, its container function is certainly very powerful.

A foreach statement is the simplest way to traverse the elements of a container. We can use the System.Collections.IEnumerator class and the System.Collections.IEnumerable interface to work with containers in C #. Here is an example of a string separator.

M://Collectionclasses\tokens.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 was a well-done program.", new char[] {', '-'});
064:foreach (string item in F)
065: {
066:console.writeline (item);
067:}
068:}
069:}
The output of this example is:
This
Is
A
So
Done
Program.
Well, that's it for the section. Now the environment is not very good, next to a large help students watching VCD, not to engage.
Next page


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.