INDEXERS (INDEXED PROPERTY)
- It is a new concept in .NET and was introduced in c# 2.0 (.NET framework 2.0)
- It is a special type of property which allows an object of class or structure to be indexed like arrays. (An object (instance) act as a virtual array)
- It is also called as smart array or virtual array
- It is possible to create regular arrays like 1D, 2D, etc, using indexer
- It supports only instance fields. It does not support static fields
- Like properties, it is implemented using get and set accessors for the [ ] operator
- Indexer modifier can be: (public / private / protected / internal)
- It is identified using its signatures. It can be overloaded.
Creation
- Indexer is created using the special keyword this (which represents the instance of the current class) and [ ] operator.
Calling (Accessing)
- Indexers are accessed by using index number (indexes)
Types
- Like arrays, it supports two types. They are
1. One Dimensional Indexer
2. Multi-Dimensional Indexer.
SYNTAX FOR 1D INDEXER (1D ARRAY)
Where,
- modifier can be private / protected / public / internal
- type can be any valid type in c# (int, float, char, etc,.)
- this represents the object of the current class
- argument list is formal argument list. Indexer definition should include at least one parameter else the compiler will provide the error message.
NOTE
- The elements of the array are filled through the indexer which is done using the special built-in property value.
- This property should be included in the set accessor of indexer.
EXAMPLE
Here, arr is an user defined array name.
DIFFERENCE BETWEEN ARRAY AND INDEXER
S.N | Array | Indexer |
1. | It is created using new modifier | It is created using the reserved word this |
2. | It is an regular array | It is an virtual array (object act as an array) which is a special property of the class |
3. | Here elements are inserted and accessed using index position | Here elements are inserted using set {} accessor with the special property value and retrieved using get{} accessor |
4. | It supports both instance and static array | It supports only instance indexer |
I. EXAMPLE OF 1D INDEXER
1. SOURCE CODE
using y = System.Console;
namespace TestApp5
{
class Program
{
// instance array variable
int[] num = new int[100];
// 1D indexer definition
public int this[int i]
{
set
{
num[i] = value;
}
get
{
return num[i];
}
}
static void Main()
{
y.WriteLine(“————————–“);
y.WriteLine(“\t 1D Indexer Indexer”);
y.WriteLine(“————————–“);
// object creation for current class
Program obj = new Program();
// calling indexer: setting values
obj[0] = 33;
obj[1] = 21;
obj[2] = 54;
obj[3] = 91;
obj[4] = 72;
// calling indexer: getting values
for(int i=0;i<obj.num.Length;i++)
{
if(obj[i]!=0)
y.WriteLine(” “+obj[i]);
}
}
}
}
2. OUTPUT
II. EXAMPLE OF 1D STRING INDEXER
AIM
- Aim of this code is used to create a 1D regular array using 1D indexer
1. SOURCE CODE
using k = System.Console;
using System;
class City
{
string[] names=new string[5]; // normal 1D array
public string this[int i]
{
// set the value at the specified index
set
{
names[i] = value;
}
// return a value at the specified index
get
{
return names[i];
}
}
static void Main()
{
k.ForegroundColor = ConsoleColor.Yellow;
k.WriteLine(“——————————-“);
k.WriteLine(“\t1D Indexer in C#”);
k.WriteLine(“——————————-“);
k.ForegroundColor = ConsoleColor.White;
// Object creation for current class
City obj = new City();
// Calling Indexer: set accessor
obj[0] = “Chennai”;
obj[1] = “Delhi”;
obj[2] = “Bangalore”;
obj[3] = “Kolkatta”;
obj[4] = “Mumbai”;
k.WriteLine(“City Names:”);
for (int i = 0; i < obj.names.Length; i++)
{
k.WriteLine(” “+(i+1)+”. “+obj[i]+” “);
}
k.WriteLine(“——————————-“);
}
}
2. OUTPUT
SYNTAX FOR 2D INDEXER (2D ARRAY)
NOTE
- The elements of the array are filled through the indexer which is done using the special built-in property value.
- This property should be included in the set accessor of indexer.
EXAMPLE
III. EXAMPLE OF 2D ARRAY CREATION USING 2D-INDEXER
AIM
- Aim of this code used to create a 2D array using 2D indexer
1. SOURCE CODE
using k=System.Console;
using System;
class TwoIndexer
{
int[,] mat=new int[3,4];
// matrix 1
public int this[int i,int j]
{
get
{
return mat[i, j];
}
set
{
mat[i, j] = value;
}
}
static void Main()
{
k.ForegroundColor = ConsoleColor.Yellow;
k.WriteLine(“——————————“);
k.WriteLine(“\t2D Indexer”);
k.WriteLine(“——————————“);
k.ForegroundColor = ConsoleColor.White;
TwoIndexer obj = new TwoIndexer();
// calling the set Indexer
// 1st row
obj[0, 0] = 5;
obj[0, 1] = 15;
obj[0, 2] = 25;
obj[0, 3] = 35;
// 2nd row
obj[1, 0] = 45;
obj[1, 1] = 55;
obj[1, 2] = 65;
obj[1, 3] = 75;
// 3rd row
obj[2, 0] = 10;
obj[2, 1] = 20;
obj[2, 2] = 30;
obj[2, 3] = 40;
k.WriteLine(“Contents of 2D Array\n”);
// calling get indexer
for (int i = 0; i < obj.mat.GetLength(0); i++)
{
k.Write(“Row #”+(i+1)+”: “);
for (int j = 0; j < obj.mat.GetLength(1); j++)
{
k.Write(obj[i, j]+” “);
}
k.WriteLine();
}
k.WriteLine(“——————————“);
}
}
2. OUTPUT
POINTS ABOUT INDEXERS
- Indexer is created using the special keyword this
- The parameterized property is called as indexer
- It is identified using its signatures and implemented using special methods like get() and set() for the [ ] operator
- It is called by using object name with index position
- It is an important to note that, it supports only instance fields (no static fields)
- In the argument list of indexer definition, at least one parameter should be required otherwise the compiler will provide an error message.
- The parameters like ref and out are not permitted in indexer definition.
MORE TUTORIALS
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…