你是想做一个类,实现用 类实例.属性[索引] 的方法访问某个集合属性是吧?那其实不是属性(虽然和属性相似), 正确的说法是“索引器”,使用方法如下://声明类的时候: using System; class IndexerClass { private int [] myArray = new int[100]; public int this [int index] // 注意,在这里声明索引器 { get { if (index < 0 || index >= 100) return 0; else return myArray[index]; } set { if (!(index < 0 || index >= 100)) myArray[index] = value; } } }public class MainClass { public static void Main() { IndexerClass b = new IndexerClass(); // 在这里使用索引器 b[3] = 256; b[5] = 1024; for (int i=0; i<=10; i++) { Console.WriteLine("Element #{0} = {1}", i, b[i]); } } }
原来是这样实现的,正在学习中
http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/collections/hashtable/hashtable.src
就可啊
2.具体代码可以使用www.google.com的论坛搜索,效果不错.不过关键字最好是E文的+csharp
什么都会有了
正确的说法是“索引器”,使用方法如下://声明类的时候:
using System;
class IndexerClass
{
private int [] myArray = new int[100];
public int this [int index] // 注意,在这里声明索引器
{
get
{
if (index < 0 || index >= 100)
return 0;
else
return myArray[index];
}
set
{
if (!(index < 0 || index >= 100))
myArray[index] = value;
}
}
}public class MainClass
{
public static void Main()
{
IndexerClass b = new IndexerClass();
// 在这里使用索引器
b[3] = 256;
b[5] = 1024;
for (int i=0; i<=10; i++)
{
Console.WriteLine("Element #{0} = {1}", i, b[i]);
}
}
}