264

例如,DataGridView 允许您执行以下操作:

DataGridView dgv = ...;
DataGridViewCell cell = dgv[1,5];

但在我的一生中,我找不到关于索引/方括号运算符的文档。他们怎么称呼它?它在哪里实施?能扔吗?我怎样才能在自己的课堂上做同样的事情?

ETA:感谢所有快速回答。简而言之:相关文档在“Item”属性下;重载的方法是声明一个像public object this[int x, int y]{ get{...}; set{...} }; 至少根据文档,DataGridView 的索引器不会抛出。它没有提到如果您提供无效坐标会发生什么。

ETA 再次:好的,即使文档没有提到它(顽皮的微软!),事实证明,如果您提供无效坐标,DataGridView 的索引器实际上会抛出 ArgumentOutOfRangeException。公平警告。

4

8 回答 8

392

你可以在这里找到如何做。简而言之就是:

public object this[int i]
{
    get { return InnerList[i]; }
    set { InnerList[i] = value; }
}

如果您只需要一个 getter,也可以使用下面答案中的语法(从 C# 6 开始)。

于 2008-11-13T19:25:19.957 回答
43

那将是项目属性:http: //msdn.microsoft.com/en-us/library/0ebtbkkc.aspx

也许这样的事情会起作用:

public T Item[int index, int y]
{ 
    //Then do whatever you need to return/set here.
    get; set; 
}
于 2008-11-13T19:26:09.440 回答
28
Operators                           Overloadability

+, -, *, /, %, &, |, <<, >>         All C# binary operators can be overloaded.

+, -, !,  ~, ++, --, true, false    All C# unary operators can be overloaded.

==, !=, <, >, <= , >=               All relational operators can be overloaded, 
                                    but only as pairs.

&&, ||                              They can't be overloaded

() (Conversion operator)            They can't be overloaded

+=, -=, *=, /=, %=                  These compound assignment operators can be 
                                    overloaded. But in C#, these operators are
                                    automatically overloaded when the respective
                                    binary operator is overloaded.

=, . , ?:, ->, new, is, as, sizeof  These operators can't be overloaded

    [ ]                             Can be overloaded but not always!

信息来源

对于支架:

public Object this[int index]
{
    
}

##但

数组索引运算符不能重载;但是,类型可以定义索引器,即采用一个或多个参数的属性。索引器参数括在方括号中,就像数组索引一样,但索引器参数可以声明为任何类型(与数组索引不同,数组索引必须是整数)。

来自MSDN

于 2008-11-13T19:23:43.860 回答
13

如果您使用的是 C# 6 或更高版本,则可以对 get-only 索引器使用表达式主体语法:

public object this[int i] => this.InnerList[i];

于 2015-12-04T22:06:58.450 回答
5
public class CustomCollection : List<Object>
{
    public Object this[int index]
    {
        // ...
    }
}
于 2008-11-13T19:25:08.443 回答
3

对于 CLI C++(使用 /clr 编译),请参阅此 MSDN 链接

简而言之,可以为属性命名为“default”:

ref class Class
{
 public:
  property System::String^ default[int i]
  {
    System::String^ get(int i) { return "hello world"; }
  }
};
于 2008-11-21T20:36:57.047 回答
2

这是一个从内部 List 对象返回值的示例。应该给你的想法。

  public object this[int index]
  {
     get { return ( List[index] ); }
     set { List[index] = value; }
  }
于 2008-11-13T19:27:48.720 回答
1

如果你的意思是数组索引器,你只需编写一个索引器属性就可以重载它。只要每个索引器属性都有不同的参数签名,你就可以重载(写多少就写多少)索引器属性

public class EmployeeCollection: List<Employee>
{
    public Employee this[int employeeId]
    {   
        get 
        { 
            foreach(var emp in this)
            {
                if (emp.EmployeeId == employeeId)
                    return emp;
            }

            return null;
        }
    }

    public Employee this[string employeeName]
    {   
        get 
        { 
            foreach(var emp in this)
            {
                if (emp.Name == employeeName)
                    return emp;
            }

            return null;
        }
    }
}
于 2008-11-13T19:29:14.753 回答