CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS MICROSOFT C# DETAILS
Question :
What is indexer? where it is used plz explain


Category C# Interview Questions
Rating (4.3) By 3 users
Added on 7/28/2006
Views 1910
Rate it!
Answers:

Indexer is a special syntax for overloading [] operator for a class. After defining an indexer, array syntaxes can be used for the class objects.



Indexer is usually used to traverse arrays. We can transfer this sort of functionality to our classes.

When creating a class of lets say Person

public class Person
{
private string name;

// create fields to hide the functionality
Public string Name
{
get{return name;}
set{name = value;}
}
// if we use this class the only one person name at a time will be saved at a time.

In order to keep a list of names we use an indexer which is used by the System.Collections namespace
iEnumberable; Icollection; IList; IDictionary

public class Persons :CollectionBase
{
public Person this[int personIndex] // indexer
{
get{return (Person)List[personIndex];}
set{List[personIndex]=value;}
}
}
Here we are using the IList.List property in the ArrayList of the CollectionBase

Of Course to add members to the list we must also add functionality by way of methods that the List.Add() and List.Remove()
portions of the class

Let me continue the Add functionality for you and I am sure that you can figure out the rest.

Public void Add(Person newPerson)
{
List.Add(newPerson); // adds the person to the list that will be accessed by the indexer
}



 Posted by: sunny    

Contact sunny  Contact sunny

An indexer is a member that enables an object to be indexed in the same way as an array.



 Posted by: Beauty    

Contact Beauty  Contact Beauty


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
can we inherit the java class in C# class,how?

View Answer
We declare an integer variable 'x' in C# as

View Answer
A code block in C# is enclosed between

View Answer
To read user input from the console, the following statement is used

View Answer
The following is a correct call to the Main() function

View Answer
All the C# programs must have the following statement

View Answer
To write a line of text to the xonsole window, we make the following call

View Answer
There must be at least the following function in a C# program

View Answer
Every statement in C# must end in

View Answer
The C# code files have an extension

View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C# Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/9087/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.98