如何在C#中重载方括号运算符?

时间:2008-11-13 19:20:23

标签: c# collections operators operator-overloading

例如,

DataGridView允许您执行此操作:

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

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

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

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

8 个答案:

答案 0 :(得分:348)

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

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

答案 1 :(得分:40)

这将是商品属性: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; 
}

答案 2 :(得分:24)

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!

Source of the information

支架:

public Object this[int index]
{

}

BUT

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

来自MSDN

答案 3 :(得分:6)

public class CustomCollection : List<Object>
{
    public Object this[int index]
    {
        // ...
    }
}

答案 4 :(得分:4)

对于CLI C ++(使用/ clr编译),请参阅this MSDN link

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

ref class Class
{
 public:
  property System::String^ default[int i]
  {
    System::String^ get(int i) { return "hello world"; }
  }
};

答案 5 :(得分:4)

如果您正在使用C#6或更高版本,则可以将表达式语法用于get-only索引器:

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

答案 6 :(得分:2)

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

  public object this[int index]
  {
     get { return ( List[index] ); }
     set { List[index] = value; }
  }

答案 7 :(得分: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;
        }
    }
}
相关问题