What is indexer? where it is used plz explain

Questions by anilputtur

Showing Answers 1 - 5 of 5 Answers

rohit

  • Nov 18th, 2005
 

6546

  Was this answer useful?  Yes

madhavi

  • Nov 14th, 2006
 

Indexers 

Indexers permit instances of a class or struct to be indexed in the same way as arrays. Indexers are similar to propeties except that their accessors take parameters.

In the following example, a generic class is defined and provided with simple get and set accessor methods as a means for assigning and retrieving values. The class Program creates an instance of this class for storing strings.

public class IndexerClass

{

private int[] arr = new int[100];

public int this[int index]

{

get

{

if (index < 0 || index >= 100)

return 0;

else

return arr[index];

}

set

{

if (!(index < 0 || index >= 100))

{

arr[index] = value;

}

}

}

static void Main(string[] args)

{

IndexerClass ic = new IndexerClass();

ic[1] = 10;

ic[2] = 20;

for (int i = 0; i <= 10; i++)

{

System.Console.WriteLine("Element #{0} = {1}", i, ic[i]);

}

Console.Read();

}

}


For more information Click here http://msdn2.microsoft.com/en-us/library/2549tw02.aspx

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions