Ylbtech-languagesamples-collectionclasses (Collection Class)

Source: Internet
Author: User

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
    1. In Solution Explorer, right-click the CollectionClasses1 project and select Set as Startup Project.

    2. On the Debug menu, click Start Without Debugging.

    3. For CollectionClasses2, repeat the previous steps.

To build and run the collection class sample from the command line
    1. Use the "Change Directory" command to go to the "CollectionClasses1" directory.

    2. Type the following command:

      CSC Tokens.cstokens
    3. Use the "Change Directory" command to go to the "CollectionClasses2" directory.

    4. Type the following command:

      CSC tokens2.cstokens2
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)

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.