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

  • 10 tips to help you become a great Pokemon trainer10 tips to help you become a great Pokemon trainer
    in the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
  • How to use GoChat application in Pokémon GOHow to use GoChat application in Pokémon GO
    pokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
  • How to play Pokemon GO in Landscape Mode on the iPhoneHow to play Pokemon GO in Landscape Mode on the iPhone
    although players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
  • The secret to controlling Pokemon Go employees at workThe secret to controlling Pokemon Go employees at work
    these days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
  • Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!Want to earn the fastest Pokécoins in Pokémon Go?  So don't miss this article!
    pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.
  • Pokémon systems when fighting in Pokémon GoPokémon systems when fighting in Pokémon Go
    each pokémon system in pokémon go has different strengths, along with a specific weakness. this type of pokémon will have the power to attack the other pokémon, but can defeat the other pokémon. if you know the characteristics of each type, it will be easier to choose which pokémon to battle.
  • The terms you need to know when playing Pokémon GoThe terms you need to know when playing Pokémon Go
    pokémon go is the most prominent name in recent days. this game of capturing and training virtual animals has created a relatively new way of playing, as players have to constantly move to catch pokémon. during the process of joining pokémon go, you will encounter and use a lot of important terms. so what do they mean?