,爲什麼我們需要:問題一個簡單的索引器(C#)
public string this[int pos]
{
get
{
return myData[pos];
}
set
{
myData[pos] = value;
}
}
究竟是什麼「這個」這個[INT POS]嗎?謝謝
/// Indexer Code Block starts here
using System;
/// <summary>
/// A simple indexer example.
/// </summary>
class IntIndexer
{
private string[] myData;
public IntIndexer(int size)
{
myData = new string[size];
for (int i = 0; i < size; i++)
{
myData[i] = "empty";
}
}
public string this[int pos]
{
get
{
return myData[pos];
}
set
{
myData[pos] = value;
}
}
static void Main(string[] args)
{
int size = 10;
IntIndexer myInd = new IntIndexer(size);
myInd[9] = "Some Value";
myInd[3] = "Another Value";
myInd[5] = "Any Value";
Console.WriteLine("\nIndexer Output\n");
for (int i = 0; i < size; i++)
{
Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
}
}
}
其他人聞到功課?如果是...它應該被標記爲這樣。 – 2009-07-29 23:35:24
它沒有功課。即時通過在線教程自學C# http://www.csharp-station.com/Tutorials/Lesson11.aspx – user133466 2009-08-01 15:38:23