YouTip LogoYouTip

Csharp Indexer

# C# Indexer **Indexer** allows an object to be accessed in a way similar to an array using an index. When you define an indexer for a class, the class behaves like a **virtual array**. You can access the class's members using the array access operator . ## Syntax The syntax for a one-dimensional indexer is as follows: element-type this { // get accessor get { // return the value specified by index } // set accessor set { // set the value specified by index } } ## Purpose of Indexers The declaration of an indexer's behavior is somewhat similar to that of a property. Like a property, you can use **get** and **set** accessors to define an indexer. However, properties return or set a specific data member, whereas indexers return or set a specific value of an instance of an object. In other words, it breaks an instance's data into smaller parts and indexes each part, getting or setting each part. Defining a property involves providing a property name. Indexers are defined without a name, but with the **this** keyword, which refers to the object instance. The following example demonstrates this concept: ## Example using System; namespace IndexerApplication { class IndexedNames { private string[] namelist =new string; static public int size =10; public IndexedNames() { for(int i =0; i =0&& index =0&& index <= size-1) { namelist=value; } } } static void Main(string[] args) { IndexedNames names =new IndexedNames(); names="Zara"; names="Riz"; names="Nuha"; names="Asif"; names="Davinder"; names="Sunil"; names="Rubic"; for(int i =0; i < IndexedNames.size; i++) { Console.WriteLine(names); } Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: ZaraRizNuhaAsifDavinderSunilRubic N. A. N. A. N. A. ## Overloading Indexers Indexers can be overloaded. Indexers can also be declared with multiple parameters, and each parameter can be of a different type. It is not necessary for indexers to be of integer type. C# allows indexers to be of other types, for example, string type. The following example demonstrates overloading an indexer: ## Example using System; namespace IndexerApplication { class IndexedNames { private string[] namelist =new string; static public int size =10; public IndexedNames() { for(int i =0; i =0&& index =0&& index <= size-1) { namelist=value; } } } public int this { get { int index =0; while(index < size) { if(namelist== name) { return index; } index++; } return index; } } static void Main(string[] args) { IndexedNames names =new IndexedNames(); names="Zara"; names="Riz"; names="Nuha"; names="Asif"; names="Davinder"; names="Sunil"; names="Rubic"; // using the first indexer with int parameter for(int i =0; i < IndexedNames.size; i++) { Console.WriteLine(names); } // using the second indexer with string parameter Console.WriteLine(names["Nuha"]); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: ZaraRizNuhaAsifDavinderSunilRubic N. A. N. A. N. A.2
← Csharp DelegateCsharp Property β†’