假设有如下类,有两个索引器,一个是整形,一个是字符串
public class Person
{
private IList favoriteNames = new ArrayList();
private IDictionary properties = new Hashtable();
public Person()
{
favoriteNames.Add("p1");
favoriteNames.Add("p2");
}
public string this[int index]
{
get { return (string) favoriteNames[index]; }
set { favoriteNames[index] = value; }
}
public string this[string keyName]
{
get { return (string) properties[keyName]; }
set { properties.Add(keyName, value); }
}
}下面的对象定义为索引器赋值
<object id="person" type="Test.Objects.Person, Test.Objects">
<property name="[0]" value="Master Shake"/>
<property name="['one']" value="uno"/>
</object>老的1.02版本写法不同,这里不再研究,直接跳过
本文介绍如何在C#中定义一个类,该类包含两个索引器,一个接受整数参数,另一个接受字符串参数。通过实例化这个类并使用提供的属性数据,展示如何通过索引器获取和设置类属性。
1644

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



