C# 定義結構的索引 感覺就像是[]的負載而以
Defining Indexers in Structures
Indexers are objects that enable a structure to be indexed in very much the
same way that
arrays do. With an indexer, you can declare several structures at the same
time and refer to
each structure using an index number. This is demonstrated in Listing 7-7,
which declares a
structure called MyStruct containing a string and an index.
Listing 7-7: Including an Indexer Within a Structure
class Listing7_7
{
struct MyStruct
{
public string []data ;
public string this [int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
}
public static void Main()
{
int x;
MyStruct ms = new MyStruct();
ms.data = new string[5];
ms[0] = "Brian D Patterson";
ms[1] = "Aimee J Patterson";
ms[2] = "Breanna C Mounts";
ms[3] = "Haileigh E Mounts";
ms[4] = "Brian W Patterson";
for (x=0;x<5;x++)
System.Console.WriteLine(ms[x]);
}
}
沒有留言:
張貼留言