C#里这样的语法效果怎么实现?

来源:百度知道 编辑:UC知道 时间:2024/06/16 03:31:22
比如:Company["外企"]="埃森哲"
或者Company["外企"].add("埃森哲")

索引器:
class Company
{
HashTable ht = new HashTable();

public object this[string key]
{
get
{
return ht[key];
}
set
{
ht[key] = value;
}
}
}

建立一个 NameValueCollection 类型即可。
命名空间: using System.Collections.Specialized;

NameValueCollection Company = new NameValueCollection();
Company["外企"] = "埃森哲";

如果你是要创建,那么可以用 this[string name] 这样的属性来创建索引器