1.

What are indexers in C# .NET? What is the difference between property & indexers?

Answer»

When we use indexers in class it behaves just LIKE the virtual array. The user sets the value of the index and can retrieve the data without POINTING an instance or a type member.

using System; class clsIndexer   {    private string[] val = new string[3];      public string this[int index]      {        get        {            return val[index];        }        set        {            val[index] = value;        }    } } class main {    public static void Main() {        clsIndexer ic = new clsIndexer();        ic[0] = "C";        ic[1] = "CPP";        ic[2] = "CSHARP";        Console.Write("Printing values stored in objects used as arrays\n");        Console.WriteLine("First value = {0}", ic[0]);        Console.WriteLine("Second value = {0}", ic[1]);        Console.WriteLine("THIRD value = {0}", ic[2]);    } }
Property
Indexers
we can call any data member of the class directly
internal collection of an object can access its elements by using array notation on the object itself
class members can be accessed by a simple name
class members can be accessed through an index
get property doesn`t have parameters
get ACCESSOR of an indexer has the same PARAMETER list as with the indexer
it can be static
it is not static


Discussion

No Comment Found