C#语法 this[string name]

chengjihao 2010-12-10 03:36:25
类里的代码片断:

public class InstanceState
{
private SortedList<string,object> atributes = null;
public object this[string name]
{
get {
return atributes[name];
}
set {
attributes[name] = value;
}
}
}
}

请问各位这是什么语法?请仔细解释,平常开发中很少见到这种
...全文
536 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
colorall 2010-12-10
  • 打赏
  • 举报
回复
索引器,属性的一种特殊形式,可以这样理解:是为了方便访问集合(SortedList)中的一个而“设计”的。
gohappy2008 2010-12-10
  • 打赏
  • 举报
回复
索引器使得对象可按照与数组相似的方法进行索引。

get 访问器返回值。set 访问器分配值。

this 关键字用于定义索引器。

value 关键字用于定义由 set 索引器分配的值。

索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。

索引器可被重载。

索引器可以有多个形参,例如当访问二维数组时。
#blackheart 2010-12-10
  • 打赏
  • 举报
回复

public class InstanceState
{
private SortedList<string,object> atributes =new SortedList<string,object>();
///索引器,可以重载
public object this[string name]
{
get {
return atributes[name];
}
set {
attributes[name] = value;
}
}
}
}

用法:
object obj=InstanceState["name"];取得SortedList<string,object> atributes对应的值
deyygywxf 2010-12-10
  • 打赏
  • 举报
回复
声明的索引器

http://msdn.microsoft.com/zh-cn/library/6x16t2tx(v=vs.80).aspx
  • 打赏
  • 举报
回复

索引器
兔子-顾问 2010-12-10
  • 打赏
  • 举报
回复
msdn 索引器
http://msdn.microsoft.com/zh-cn/library/6x16t2tx(v=vs.80).aspx
  • 打赏
  • 举报
回复
实现索引器http://www.cnblogs.com/luminji/archive/2010/09/11/1823962.html
TimZhuFaith 2010-12-10
  • 打赏
  • 举报
回复
private SortedList<string,object> atributes = new SortedList<string,object>();
寒似火 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jayinit 的回复:]
声明的索引器!
[/Quote]
+1
TimZhuFaith 2010-12-10
  • 打赏
  • 举报
回复
就是索引器,带参数的属性。。。lz代码也不对,没有实例化
  • 打赏
  • 举报
回复
声明的索引器!

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧