繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> 老外编的程序(九):Using Indexer Properties

老外编的程序(九):Using Indexer Properties

2007-07-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:// indexedproperty.cs using System; public class Document { // Type allowing the document to be viewed like an array of words: public class wordCollection { readonly Document document; // The con...

// indexedproperty.cs

using System;

public class Document

{

// Type allowing the document to be viewed like an array of words:

public class wordCollection

{

readonly Document document; // The containing document

internal wordCollection(Document d)

{

document = d;

}

// Helper function -- search character array "text", starting at

// character "begin", for word number "wordCount." Returns false

// if there are less than wordCount words. Sets "start" and

// length" to the position and length of the word within text:

private bool Getword(char[] text, int begin, int wordCount,

out int start, out int length)

{

int end = text.Length;

int count = 0;

int inword = -1;

start = length = 0;

for (int i = begin; i <= end; ++i)

{

bool isLetter = 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;

}

// Indexer to get and set words of the containing document:

public string this[int index]

{

get

{

int start, length;

if (Getword(document.TextArray, 0, index, out start,

out length))

return new string(document.TextArray, start, length);

else

throw new IndexOutOfRangeException();

}

set

{

int start, length;

if (Getword(document.TextArray, 0, index, out start,

out length))

{

// Replace the word at start/length with the

// string "value":

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 new IndexOutOfRangeException();

}

}

// Get the count of words in the containing document:

public int Count

{

get

{

int count = 0, start = 0, length = 0;

while (Getword(document.TextArray, start + length, 0,

out start, out length))

++count;

return count;

}

}

}

// Type allowing the document to be viewed like an "array"

// of characters:

public class CharacterCollection

{

readonly Document document; // The containing document

internal CharacterCollection(Document d)

{

document = d;

}

// Indexer to get and set characters in the containing document:

public char this[int index]

{

get

{

return document.TextArray[index];

}

set

{

document.TextArray[index] = value;

}

}

// Get the count of characters in the containing document:

public int Count

{

get

{

return document.TextArray.Length;

}

}

}

// Because the types of the fields have indexers,

// these fields appear as "indexed properties":

public readonly wordCollection words;

public readonly CharacterCollection Characters;

private char[] TextArray; // The text of the document.

public Document(string initialText)

{

TextArray = initialText.ToCharArray();

words = new wordCollection(this);

Characters = new CharacterCollection(this);

}

public string Text

{

get

{

return new string(TextArray);

}

}

}

class Test

{

static void Main()

{

Document d = new Document(

"peter piper picked a peck of pickled peppers. How many pickled peppers did peter piper pick?"

);

// Change word "peter" to "penelope":

for (int i = 0; i < d.words.Count; ++i)

{

if (d.words[i] == "peter")

d.words[i] = "penelope";

}

// Change character "p" to "P"

for (int i = 0; i < d.Characters.Count; ++i)

{

if (d.Characters[i] == 'p')

d.Characters[i] = 'P';

}

Console.WriteLine(d.Text);

}

}

责任编辑:admin
相关文章