Concept of Indexer

G

Guest

Hello!

I am quite new to C#, and one concept that really gives me a headache is
"indexer". I have gone through the MSDN examples, and, at some level know how
to use indexers. But, the thing is I do not understand the specification that
says something along this line "with indexers you can index objects of a
class like an array". The thing is, in all the examples, there is only one
"new" call when only one object is created! There is no array of objects.
Yet, right after the "new" call, indexer is used, like obj[0], obj[1],
obj[2], ..., objNo. When these objects are instantiated? Also, I have tried
printing messages through contructor, hoping that array-like reference
through the indexer will invoke a constructor for each object in the array,
but it did not. Only once that happened when the object is first (and only
time, it appears) was created.

Any explanation? Is there arereally "n" objects created from the class that
has declared an "indexer"?

Thanks for the help!

Nash,
Toronto
 
J

JT

Can you include specific code for your declaration and how you're
trying to use it? Maybe include the example code you're following,
too.

string declarations don't require any special syntax, but once assigned
can be referenced like character arrays, like in the old C and C++
days. I get the feeling you're not referring to strings though. Some
other declarations do have some weird syntax though.
 
C

Chris Chilvers

A good example of a class using an indexer is ArrayList.

//here we create an ArrayList object, note it is only one object
ArrayList list = new ArrayList();

//note we access it like an array even though it is not
list[0] = "test";

Notice this it uses a normal constructor unlike that of an array, and
the result is you only get one object, not an array of objects.

Internally the array list may choose to use an array to store its data,
or it could use a linked list, or any other form it chooses, but this is
up to the object.

If you were to define the ArrayList like:
ArrayList lists[] = new ArrayList[5];

what you have in fact created is an array of ArrayLists, this is no
different (other than strong typing) than having defined:
object obj[] = new object[5];

And as such the array, lists[], will initialy contain nothing but null
until you assign each element a new ArrayList.

An indexer just simply allows you to use things like:
myObject[5]
myObject["test"]
etc.

As opposed to having to write two methods such as:
object GetValue(int index)
SetValue(int index, object value)

The index can be any type the class' author wishes to use, such as the
HashTable where the indexer (the key) is an object.

Likewise the return value can be anything the class' author wishes to
use.

On Tue, 9 May 2006 16:29:02 -0700, Nash Alexx <Nash
 
G

Guest

Thank you, Chris! It really helped!
Thanks, as well, to all who participated in the brainstorming!

Nash


Chris Chilvers said:
A good example of a class using an indexer is ArrayList.

//here we create an ArrayList object, note it is only one object
ArrayList list = new ArrayList();

//note we access it like an array even though it is not
list[0] = "test";

Notice this it uses a normal constructor unlike that of an array, and
the result is you only get one object, not an array of objects.

Internally the array list may choose to use an array to store its data,
or it could use a linked list, or any other form it chooses, but this is
up to the object.

If you were to define the ArrayList like:
ArrayList lists[] = new ArrayList[5];

what you have in fact created is an array of ArrayLists, this is no
different (other than strong typing) than having defined:
object obj[] = new object[5];

And as such the array, lists[], will initialy contain nothing but null
until you assign each element a new ArrayList.

An indexer just simply allows you to use things like:
myObject[5]
myObject["test"]
etc.

As opposed to having to write two methods such as:
object GetValue(int index)
SetValue(int index, object value)

The index can be any type the class' author wishes to use, such as the
HashTable where the indexer (the key) is an object.

Likewise the return value can be anything the class' author wishes to
use.

On Tue, 9 May 2006 16:29:02 -0700, Nash Alexx <Nash
Hello!

I am quite new to C#, and one concept that really gives me a headache is
"indexer". I have gone through the MSDN examples, and, at some level know how
to use indexers. But, the thing is I do not understand the specification that
says something along this line "with indexers you can index objects of a
class like an array". The thing is, in all the examples, there is only one
"new" call when only one object is created! There is no array of objects.
Yet, right after the "new" call, indexer is used, like obj[0], obj[1],
obj[2], ..., objNo. When these objects are instantiated? Also, I have tried
printing messages through contructor, hoping that array-like reference
through the indexer will invoke a constructor for each object in the array,
but it did not. Only once that happened when the object is first (and only
time, it appears) was created.

Any explanation? Is there arereally "n" objects created from the class that
has declared an "indexer"?

Thanks for the help!

Nash,
Toronto
 
V

V

Hi,

Just wanted to clarify one thing (for someone who might be a newbie):
//here we create an ArrayList object, note it is only one object
ArrayList list = new ArrayList();
//note we access it like an array even though it is not
list[0] = "test";

In the above example, you will get an error thrown until unless there
are already at least 3 elements in the ArrayList (either you have added
them by calling list.Add(object) 3 times, or maybe by instantiating the
Arraylist to have at least 3 elements to begin with.

Regards,
Vaibhav
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top