Indexers,A Short Note.

sql-index
sql-index

Image courtesy of Grant Cochrane / FreeDigitalPhotos.net

Indexers,A Short Note.

Welcome to CodeSpread!

What is an indexer?

Indexers can be easily remembered as arrays. These arrays are special as they allow the class to be treated as arrays and access the data stored within the object. For example:


class ClassCollections

{

static void Main(string[] args)

{

NewCollection newCollection = new NewCollection();

newCollection [0] = “Hello” ;

newCollection [1]= “World”;

Console.WriteLine(newCollection [0]+newCollection [1]);

}

}

The main advantage of indexer is, there is no dependency on the internal implementation of the class and data can be obtained easily through the use of indexers.

How can we declare an indexer?

Indexers are more or less like properties except that their accessors take parameters. Here, instead of using a name for the properties, we use the ‘this’ keyword, which refers to the current instance. For example:


class NewCollection<T>

{

private const int SIZE = 100;

private T[] arr = new T[SIZE];

//Declare Indexer using ‘this’ keyword.

public T this[int i]

{

get

{

return arr[i];

}

set

{

arr[i] = value;

}

}

}

Note: In.Net, All iterative collections and searchable collections use indexers.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Note: GZipStreamNote: GZipStreamA short note from my experience: From the name itself you can at least make a guess about the functionality of the GZipStream class(present under: System.IO.Compression). Yes..your guess is abso...
  • My first experience with LISTS in C#My first experience with LISTS in C#Hi guys, this is Sathish, Lets discuss the concepts of lists. These are the concepts which I have planned to discuss with you guys today. What is a List and how it is advantageous than array...
  • C# – Box(ing) and Un-box(ing)C# – Box(ing) and Un-box(ing)I have heard this concept from many people and their different versions so I always wanted to share some insight on it which can work as a one stop solution. What is boxing and un-boxing? The Con...
  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...
  • base and this keyword in C#base and this keyword in C#I have seen programmers getting confused with keywords like 'base' and 'this'. Although self-explanatory, still requires basic knowledge of scope to dive in deep. Let's take them one by one. ba...
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • C#: Anonymous Type is not anonymous!C#: Anonymous Type is not anonymous!Why Anonymous Type? Anonymous types provide you the ease of having set of properties (Read only) into a single object without knowing the type. It’s the job of compiler to generate the type inform...
  • 23 Dec: Must Read Codes [11-15]23 Dec: Must Read Codes [11-15]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeRe...