Friday, May 22, 2009

Indexer

Defining an indexer allows you to create classes that act like "virtual arrays." Instances of that class can be accessed using the [] array access operator. Defining an indexer in C# is similar to defining operator [] in C++, but is considerably more flexible. For classes that encapsulate array- or collection-like functionality, using an indexer allows the users of that class to use the array syntax to access the class.

Example

The following example shows how to declare a private array field, myArray, and an indexer. Using the indexer allows direct access to the instance b[i]. The alternative to using the indexer is to declare the array as a public member and access its members, myArray[i], directly.

// cs_keyword_indexers.cs
using System;
class IndexerClass
{
private int [] myArray = new int[100];
public int this [int index] // Indexer declaration
{
get
{
// Check the index limits.
if (index <>= 100)
return 0;
else
return myArray[index];
}
set
{
if (!(index <>= 100))
myArray[index] = value;
}
}
}

public class MainClass
{
public static void Main()
{
IndexerClass b = new IndexerClass();
// Call the indexer to initialize the elements #3 and #5.
b[3] = 256;
b[5] = 1024;
for (int i=0; i<=10; i++)
{
Console.WriteLine("Element #{0} = {1}", i, b[i]);
}
}
}

5 comments:

Anonymous said...

We understand that must be quite disappointing when you miss money and also you become cannot enjoy the get-togethers of the festival with your family in how you want [url=http://www.lshru.co.uk/]http://www.quickloansjellyfish.co.uk/[/url] uk quick loans The applicant has to make the request with the help of online http://www.lshru.co.uk/

Anonymous said...

pirate flag ballroom CasinoBlackjack dance palace has managed to cater its players it not entirely differs on dissimilar casinos and games, but besides varies over a menstruum of fourth dimension. [url=http://www.qouar.co.uk/]woohooonlinecasino.co.uk[/url] casino online We are providing Here open to existing Ladbrokes customers who Receive Ne'er played casino ahead. http://www.mecrk.co.uk/

Anonymous said...

Many of the other reasons on this list would happen to anyone at any point in someone's academic profession [url=http://www.pevmb.co.uk/]uk short term loans[/url] http://www.1dayshorttermloans.co.uk/ You'll have to learn about something which provokes your crowd and that will definitely not set you back an excessive amount of http://www.vsxbu.co.uk/

Anonymous said...

Good day! I just want to give a huge thumbs up for the great information you could have right here on this post.
I shall be coming back to your blog for extra soon.

Feel free to visit my web-site - blackbird lyrics alter bridge youtube

Anonymous said...

It is only natural this family members should help 1 another out by means of lending money if they are equipped Anthony Richard Clarke "