Ylbtech-languagesamples-indexers_2 (Indexer)

Source: Internet
Author: User

Ylbtech-microsoft-csharpsamples:ylbtech-languagesamples-indexers_2 (Indexer)

1.A, example (sample) back to top
indexers_2 Example

This example shows how a C # class declares an indexer to represent a collection of similar arrays of different kinds of things. For additional information, see Properties (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 Indexers_2 sample in Visual Studio
    • On the Debug menu, click Start Without Debugging.

To build and run the Indexers_2 sample from the command line
    • At the command prompt, type the following command:

      CSC indexers_2.csindexers_2
1.B, sample code back to top

1.b.1, Indexers_2.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. //Indexedproperty.csusingSystem; Public classdocument{//The following types allow you to view documents in a similar way to a group of words:     Public classwordcollection {ReadOnlyDocument document;//include document        Internalwordcollection (document D) {document=D; }        //Helper Function--Search in the character array "text" starting from the character "begin"//the word "wordCount". If less than//WordCount The number of words, false is returned. The "Start" and//"Length" is set to the position and length of the word in the text:        Private BOOLGetword (Char[] text,intBeginintWordCount, out intStart out intlength) {             intEnd =text.            Length; intCount =0; intInword =-1; Start= length =0;  for(inti = begin; I <= end; ++i) {BOOLIsletter = i < end &&char.isletterordigit (Text[i]); if(Inword >=0)                 {                    if(!isletter) {                        if(count++ = =WordCount) {Start=Inword; Length= i-Inword; return true; } Inword= -1; }                }                Else                 {                    if(isletter) Inword=i; }            }            return false; }        //gets and sets the indexer that contains the words in the document:         Public string  This[intIndex] {            Get             {                 intstart, length; if(Getword (document). Textarray,0, Index, outStart, outlength)) return New string(document.                Textarray, start, length); Else                    Throw NewIndexOutOfRangeException (); }            Set             {                intstart, length; if(Getword (document). Textarray,0, Index, outStart, outlength)) {                    //Replace the string "value" with the Start/length at//Word:                    if(Length = =value. Length) {array.copy (value. ToCharArray (),0, document.                    Textarray, start, length); }                    Else                     {                        Char[] NewText =New Char[Document. Textarray.length +value. Length-length]; Array.copy (document. Textarray,0, NewText,0, start); Array.copy (value. ToCharArray (),0, NewText, start, value.                        Length); Array.copy (document. Textarray, start+length, NewText, start+value. Length, document. Textarray.length-Start-length); Document. Textarray=NewText; }                }                                    Else                    Throw NewIndexOutOfRangeException (); }        }        //gets the count of words in the containing document:         Public intCount {Get             {                 intCount =0, start =0, length =0;  while(Getword (document). Textarray, start + length,0,                                                outStart outlength)) ++count; returncount; }        }    }    //the following types allow viewing of documents in a similar character array// :     Public classcharactercollection {ReadOnlyDocument document;//include document        Internalcharactercollection (document D) {document=D; }        //gets and sets the indexer that contains the characters in the document:         Public Char  This[intIndex] {            Get             {                 returndocument.             Textarray[index]; }            Set{document. Textarray[index]=value; }        }        //gets the count of characters in the containing document:         Public intCount {Get             {                 returndocument.             Textarray.length; }        }    }    //because the type of the field has an indexer,//Therefore, these fields appear as indexed properties:     Publicwordcollection Words;  Publiccharactercollection characters; Private Char[] Textarray;//the text of the document.      PublicDocument (stringInitialtext) {Textarray=Initialtext.tochararray (); Words=NewWordcollection ( This); Characters=NewCharactercollection ( This); }     Public stringText {Get         {            return New string(Textarray); }    }}classtest{Static voidMain () {Document D=NewDocument ("Peter Piper picked a peck of pickled peppers. How many pickled peppers did Peter Piper pick?"        ); //Change the word "Peter" to "Penelope":         for(inti =0; i < D.words.count; ++i) {if(D.words[i] = ="Peter") D.words[i]="Penelope"; }        //change the character "P" to "P "         for(inti =0; i < D.characters.count; ++i) {if(D.characters[i] = ='P') D.characters[i]='P';    } Console.WriteLine (D.text); }}
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-indexers_2 (Indexer)

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.