W e b t u t o r i a l s

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Exercise-1

Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

Exercise on Loops and Nested Loop

C# Arrays

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

C# Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Interface

Abstract Classes, Abstract Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

System.Object

Indexer

An indexer allows an object to be indexed like an array. When you define an indexer for a class, this class behaves like a virtual array. You can then access the instance of this class using the array access operator ([ ]).

Use of Indexers
Declaration of behavior of an indexer is to some extent similar to a property. Like properties, you have get and set accessors for defining an indexer. However, properties return or set a specific data member, whereas indexers returns or sets a particular value from the object instance. In other words, it breaks the instance data into smaller parts and indexes each part, gets or sets each part.
Defining a property involves providing a property name. Indexers are not defined with names, but with this keyword, which refers to the object instance. The following example demonstrates the concept:

    class ex_indexer
    {
	    private string[] name = new string[10];
	    public string this[int index]
	    {
		    get
		    {
			    string s = name[index];
			    return s;
		    }
		    set
		    {
			    name[index] = value;
		    }
	    }
    }
    class Program
    {
	    static void Main(string[] args)
	    {
		    ex_indexerobj = new ex_indexer();
		    obj[0] = "ankit";
		    obj[1] = "ankeet";
		    for(int i = 0;i<=9;i++)
		    {
			    Console.WriteLine(obj[i]);
		    }
		    Console.ReadKey();
	    }
    }