public A this[i]是什么?

Zeroing-X 2010-07-05 09:53:18
public Point this[int index
{
get { return structArr[index]; }
}

这个是什么意思。。。我看大概能知道它的作用。。但是不明白具体是什么?
是属性吗?
属性可以这样传参吗?
...全文
63 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-07-05
  • 打赏
  • 举报
回复
索引器
看看msdn
索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。
public int this[int index]
{
get {}
set {}
}
using System;
class IndexerClass
{
private int[] arr = new int[100];
public int this[int index]
{
get
{
if (index < 0 || index >= 100)
{
return 0;
}
else
{
return arr[index];
}
}
set
{
if (!(index < 0 || index >= 100))
{
arr[index] = value;
}
}
}
}

class MainClass
{
static void Main()
{
IndexerClass test = new IndexerClass();
test[3] = 256;
test[5] = 1024;
for (int i = 0; i <= 10; i++)
{
Console.WriteLine("Element {0} = {1}", i, test[i]);
}
}
}
wuyi8808 2010-07-05
  • 打赏
  • 举报
回复
索引器

110,566

社区成员

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

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

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