Ylbtech-microsoft-csharpsamples:ylbtech-languagesamples-collectionclasses (Collection Class) |
1.A, example (sample) back to top |
Collection Class sample
This example shows how to implement a collection class that can be used with a foreach statement. For more information, see collection classes (C # Programming Guide).
Safety Instructions |
This code example is provided to illustrate a concept that does not represent the most secure coding practice, so you should not use this code example in an application or Web site. Microsoft assumes no responsibility for incidental or consequential damages arising from the use of this code sample for other purposes. |
To build and run the collection class sample in Visual Studio
In Solution Explorer, right-click the CollectionClasses1 project and select Set as Startup Project.
On the Debug menu, click Start Without Debugging.
For CollectionClasses2, repeat the previous steps.
To build and run the collection class sample from the command line
Use the "Change Directory" command to go to the "CollectionClasses1" directory.
Type the following command:
Use the "Change Directory" command to go to the "CollectionClasses2" directory.
Type the following command:
1.B, sample code back to top |
1.b.1, Tokens.cs
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //Tokens.csusingSystem;//Mission name Space System.Collections available:usingSystem.Collections;//declaring the Tokens class: Public classtokens:ienumerable{Private string[] elements; Tokens (stringSourceChar[] delimiters) { //parse the string into tokens:elements =source. Split (delimiters); } //IEnumerable Interface Implementation://declare the required IEnumerable//GetEnumerator () method PublicIEnumerator GetEnumerator () {return NewTokenenumerator ( This); } //The inner class implements the IEnumerator interface: Private classTokenenumerator:ienumerator {Private intPosition =-1; PrivateTokens t; Publictokenenumerator (Tokens t) { This. T =T; } //declare the MoveNext method required for IEnumerator: Public BOOLMoveNext () {if(Position < t.elements.length-1) {Position++; return true; } Else { return false; } } //declare the Reset method required for IEnumerator: Public voidReset () {position= -1; } //declare the current property required for IEnumerator: Public ObjectCurrent {Get { returnT.elements[position]; } } } //Test Mark Tokenenumerator Static voidMain () {//test the markup by decomposing the string into tokens:Tokens f =NewTokens ("This was a well-done program.", New Char[] {' ','-'}); foreach(stringIteminchf) {Console.WriteLine (item); } }}
View Code
1.b.2,
1.B, sample code 2 (sample codes) back to top |
1.b.1, Tokens2.cs
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //Tokens2.csusingSystem;usingSystem.Collections; Public classtokens:ienumerable{Private string[] elements; Tokens (stringSourceChar[] delimiters) {Elements=source. Split (delimiters); } //IEnumerable Interface Implementation: PublicTokenenumerator GetEnumerator ()//non-IEnumerable version { return NewTokenenumerator ( This); } IEnumerator ienumerable.getenumerator ()//IEnumerable version { return(IEnumerator)NewTokenenumerator ( This); } //The inner class implements the IEnumerator interface: Public classTokenenumerator:ienumerator {Private intPosition =-1; PrivateTokens t; Publictokenenumerator (Tokens t) { This. T =T; } Public BOOLMoveNext () {if(Position < t.elements.length-1) {Position++; return true; } Else { return false; } } Public voidReset () {position= -1; } Public stringCurrent//Non-IEnumerator version: Type-safe { Get { returnT.elements[position]; } } ObjectIEnumerator.Current//IEnumerator Version: Return object { Get { returnT.elements[position]; } } } //Test Mark Tokenenumerator Static voidMain () {Tokens F=NewTokens ("This was a well-done program.", New Char[] {' ','-'}); foreach(stringIteminchF//to change a string to int{Console.WriteLine (item); } }}
View Code
1.b.2,
1.C, (free Download) back to top |
|
Ylbtech Source: http://ylbtech.cnblogs.com/ This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. |
Ylbtech-languagesamples-collectionclasses (Collection Class)