C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Implementation of Indexers in C#


Posted Date: 22 Feb 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Babu A       Member Level: Gold
Rating:     Points: 5



Introduction



C# Indexers provide a way to access classes or properties (usually a specialized collection of objects) using standard array index syntax.

Indexers are implemented thru get and set accessors for the [ ] operator.

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.

Sample Program




using System;

class IndexExample
{
string Message;

public static void Main()
{
IndexExample obj=new IndexExample("Welcome");


for(int i=0;i < obj.Length;i++)
{
Console.WriteLine(obj[i]);
}

obj[obj.Length-1]="Test";

Console.WriteLine(obj.Message);

}

public IndexExample(string s)
{
Message=s;
}

public string this[int i]
{
get
{
if(i >= 0 && i < Message.Length)
{
return Message.Substring(i,1);
}
else
{
return "";
}
}
set
{
if(i >= 0 && i < Message.Length)
{
Message=Message.Substring(0,i) + value + Message.Substring(i+1);
}
}
}

public int Length
{
get
{
if(Message!=null)
{
return Message.Length;
}
else
return 0;
}
}
}



Summary



1. Indexers enable objects to be indexed in a similar manner to arrays.

2. A get accessor returns a value. A set accessor assigns a value.

3. The this keyword is used to define the indexers.

4. The value keyword is used to define the value being assigned by the set indexer.

5. Indexers do not have to be indexed by an integer value; it is up to you how to define the specific look-up mechanism.

6. Indexers can be overloaded.

7. Indexers can have more than one formal parameter, for example, when accessing a two-dimensional array.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Indexers uses  .  Indexers introduction  .  Indexers in C#  .  Indexers Example  .  C# Indexers  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Improving Performance with Connection Pooling
Previous Resource: Accessing data using Language Integrated Query (LINQ) in C# 3.0 and later
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing

Contact Us    Privacy Policy    Terms Of Use