class
SampleCollection
<
T
>
{
private T[] arr = new T[ 100 ];
public T this [ int i]
{
get
{
return arr[i];
}
set
{
arr[i] = value;
}
}
}
// This class shows how client code uses the indexer
class Program
{
static void Main( string [] args)
{
SampleCollection < string > stringCollection = new SampleCollection < string > ();
stringCollection[ 0 ] = " Hello, World " ;
System.Console.WriteLine(stringCollection[ 0 ]);
}
}
{
private T[] arr = new T[ 100 ];
public T this [ int i]
{
get
{
return arr[i];
}
set
{
arr[i] = value;
}
}
}
// This class shows how client code uses the indexer
class Program
{
static void Main( string [] args)
{
SampleCollection < string > stringCollection = new SampleCollection < string > ();
stringCollection[ 0 ] = " Hello, World " ;
System.Console.WriteLine(stringCollection[ 0 ]);
}
}
本文介绍了一个使用泛型实现的简单集合类SampleCollection,该类通过索引器提供元素的读取与设置功能。示例展示了如何创建并使用这个集合类来存储和检索字符串。

3058

被折叠的 条评论
为什么被折叠?



