Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, April 21, 2016

How do I overload the square-bracket operator in C#?

How do I overload the square-bracket operator in C#?


DataGridView, for example, lets you do this:

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

but for the life of me I can't find the documentation on the index/square-bracket operator. What do they call it? Where is it implemented? Can it throw? How can I do the same thing in my own classes?

ETA: Thanks for all the quick answers. Briefly: the relevant documentation is under the "Item" property; the way to overload is by declaring a property like public object this[int x, int y]{ get{...}; set{...} }; the indexer for DataGridView does not throw, at least according to the documentation. It doesn't mention what happens if you supply invalid coordinates.

ETA Again: OK, even though the documentation makes no mention of it (naughty Microsoft!), it turns out that the indexer for DataGridView will in fact throw an ArgumentOutOfRangeException if you supply it with invalid coordinates. Fair warning.

Answer by Patrick Desjardins for How do I overload the square-bracket operator in C#?


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

For bracket:

public Object this[int index]  {    }  

BUT

The array indexing operator cannot be overloaded; however, types can define indexers, properties that take one or more parameters. Indexer parameters are enclosed in square brackets, just like array indices, but indexer parameters can be declared to be of any type (unlike array indices, which must be integral).

From MSDN

Answer by Jason Miesionczek for How do I overload the square-bracket operator in C#?


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

Answer by Ruben for How do I overload the square-bracket operator in C#?


you can find how to do it here. In short it is:

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

Answer by Ricardo Villamil for How do I overload the square-bracket operator in C#?


That would be the item property: http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx

Maybe something like this would work:

public T Item[int index, int y]  {       //Then do whatever you need to return/set here.      get; set;   }  

Answer by Rob Prouse for How do I overload the square-bracket operator in C#?


Here is an example returning a value from an internal List object. Should give you the idea.

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

Answer by Charles Bretana for How do I overload the square-bracket operator in C#?


If you mean the array indexer,, You overload that just by writing an indexer property.. And you can overload, (write as many as you want) indexer properties as long as each one has a different parameter signature

public class EmpployeeCollection: List  {      public Employee this[int employeeId]      {             get           {               foreach(Employee emp in this)                  if (emp.EmployeeId == employeeId) return emp;              return null;          }      }      public Employee this[string employeeName]        {             get           {               foreach(Employee emp in this)                  if (emp.name == employeeName) return emp;              return null;          }      }    }  

Answer by superjordo for How do I overload the square-bracket operator in C#?


For CLI C++ (compiled with /clr) see this MSDN link.

In short, a property can be given the name "default":

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

Answer by amoss for How do I overload the square-bracket operator in C#?


If you're using C# 6 or later, you can use expression-bodied syntax for get-only indexer:

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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.