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 } }
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
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 #