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 !




Indexers


Posted Date: 25 Apr 2008    Resource Type: Code Snippets    Category: C# Syntax

Posted By: VijayKumar Reddy M       Member Level: Gold
Rating:     Points: 10



Indexers are a new concept in C#. Indexers enable a class object to function as an array. Implementing indexers is similar to implementing properties using the get and set functions. The only different is that when you call an indexer, you pass an indexing parameter. Accessing an indexer is similar to accessing an array. Indexers are nameless, so the this keyword declares indexers.

I just said that after defining indexers, a class object could be treated as an array. What does that mean? To explain, I‘ll show you an example using the class called my class. The way you treat an instance of myClass now is like this:


myClass cls = new myClass();
cls.MaleGender = true;

After defining an indexer in myClass, you could treat an instance of it as if it were an array:

myClass cls = new myClass();
cls[0].MaleGender = true;
cls[1].MaleGender = true;

You define indexer by using the this keyword as if were an array property of type object.

public object this[int index]
{
get
{
if (! ValidIndex(index))
throw new Exception("Index out of range.");
else
return MaleGender(index).Value;
}
set
{
if (!ValidIndex(index) )
throw new Exception("Index out of range.");
else
MaleGender(index).Value = value;
}
}









Responses


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

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

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: How do I get the path to my running EXE?
Previous Resource: Method overloading example
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use