C#中静态索引器的可能性 [英] Possibility of static indexers in C#
本文介绍了C#中静态索引器的可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么C#索引器必须是实例成员?它们也可以是静态成员吗?
Why is it that C# indexers have to be instance members? Can they be static members too?
推荐答案
否,但是您可以非常接近.参见以下示例:
No, but you can come pretty close. See this example:
class Test
{
static Dictionary<int, string> map = new Dictionary<int, string>();
private Test() { }
private static Test instance = new Test();
internal static Test Instance
{
get { return Test.instance; }
}
public string this[int i]
{
get
{
return map[i];
}
set
{
map[i] = value;
}
}
}
用法示例:
Sample usage:
Test.Instance[3] = "hello";
string s = Test.Instance[3];
这篇关于C#中静态索引器的可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文