Indexer in C #

The ndexer in C # helps index objects, such as an array. When you define an indexer for a class, this class operates similarly to a virtual array . Then you can access the instance of this class with the array access operator in C # ([]).

Indexer syntax

In C #, one-way Indexer has the following syntax:

 
ki ể u_ph ầ n_t ử this [ int index ] { get { // trả về giá trị được xác định bởi index } set { // thiết lập giá trị được xác định bởi index } }

Use Indexer in C #

Indexer behavior declaration is similar to attribute declaration. You use get and set to define an Indexer. However, the properties return or set a specific data member, but, the Indexer returns or sets a specific value from the instance of the object. In other words, it divides instance data into smaller parts and indexes each section, grabs or sets each section.

The difference is, you need to provide the attribute name when declaring the attribute, and the Indexer needs the name, you only need the this keyword to refer to the instance of the object. The following example illustrates this:

 using System ; namespace QTMCSharp { class IndexedNames { private string [] danhSachTen = new string [ size ]; static public int size = 10 ; public IndexedNames () { for ( int i = 0 ; i < size ; i ++) danhSachTen [ i ] = "N. A." ; } public string this [ int index ] { get { string tmp ; if ( index >= 0 && index <= size - 1 ) { tmp = danhSachTen [ index ]; } else { tmp = "" ; } return ( tmp ); } set { if ( index >= 0 && index <= size - 1 ) { danhSachTen [ index ] = value ; } } } static void Main ( string [] args ) { IndexedNames names = new IndexedNames (); names [ 0 ] = "Anh" ; names [ 1 ] = "Hoàng" ; names [ 2 ] = "Hoa" ; names [ 3 ] = "Thám" ; names [ 4 ] = "Bình" ; names [ 5 ] = "Định" ; names [ 6 ] = "Vương" ; for ( int i = 0 ; i < IndexedNames . size ; i ++ ) { Console . WriteLine ( names [ i ]); } Console . ReadKey (); } } } 

Compiling and running the C # program on your side will have the following results:

 Brother 
Hoang
flower
Detective
jar
Dinh
Wang
NA
NA
NA

Load Indexer stack in C #

Indexer can be overloaded in C #. Indexer can also be declared with multiple parameters and each parameter can be a different type. Indexers are not necessarily integers. C # allows indexes to be other value types, such as strings.

The following example will illustrate how to load Indexer in C #:

 using System ; namespace IndexerApplication { class IndexedNames { private string [] danhSachTen = new string [ size ]; static public int size = 10 ; public IndexedNames () { for ( int i = 0 ; i < size ; i ++) { danhSachTen [ i ] = "N. A." ; } } public string this [ int index ] { get { string tmp ; if ( index >= 0 && index <= size - 1 ) { tmp = danhSachTen [ index ]; } else { tmp = "" ; } return ( tmp ); } set { if ( index >= 0 && index <= size - 1 ) { danhSachTen [ index ] = value ; } } } public int this [ string name ] { get { int index = 0 ; while ( index < size ) { if (danhSachTen [ index ] == name ) { return index ; } index ++; } return index ; } } static void Main ( string [] args ) { IndexedNames names = new IndexedNames (); names [ 0 ] = "Anh" ; names [ 1 ] = "Hoàng" ; names [ 2 ] = "Hoa" ; names [ 3 ] = "Thám" ; names [ 4 ] = "Bình" ; names [ 5 ] = "Định" ; names [ 6 ] = "Vương" ; //sử dụng indexer đầu tiên với tham số int for ( int i = 0 ; i < IndexedNames . size ; i ++) { Console . WriteLine ( names [ i ]); } //sử dụng indexer thứ 2 với tham số string Console . WriteLine ( names [ "Vương" ]); Console . ReadKey (); } } } 

Running the code above we will get the following result:

 Brother 
Hoang
flower
Detective
jar
Dinh
Wang
NA
NA
NA
6

According to Tutorialspoint

Previous article: Reflection in C #

Next article: Delegate in C #

5 ★ | 1 Vote

May be interested

  • Delegate in C #Photo of Delegate in C #
    delegate in c # is similar to pointers to functions, in c or in c ++. delegate is a reference type variable that contains references to a method. that reference can be changed at runtime.
  • Event (Event) in C #Photo of Event (Event) in C #
    events (events) are the actions of users, such as pressing keys, clicking, moving mouse ... applications need to respond to these events when they appear. for example, interrupts. events (events) are used to communicate within the process.
  • Collection in C #Photo of Collection in C #
    collection classes are special classes for storing and retrieving data. these classes provide support for stack, queue, list, and hash table. most collection classes in c # deploy the same interface.
  • Generic in C #Photo of Generic in C #
    generic in c # allows you to delay the specification (specification) of the data type of programming elements in a class or method, until it is actually used in the program. in other words, generic allows you to write a class or method that can work with any data type.
  • Anonymous method in C #Photo of Anonymous method in C #
    we have discussed that delegate is used to reference any method that has the same sign as in delegate. in other words, you can call a method that can be referenced by a delegate using that delegate object.
  • Safe code in C #Photo of Safe code in C #
    c # allows using pointer variables in a function of code block when it is marked by unsafe modifier. the concept of unsafe code or unmanaged code in c # is a code block that uses a pointer variable.