what is the point when the ArrayList return an int

T

Tony Johansson

Hi!

I just wonder what is the point to return an int when you add an element to
this ArrayList ?
Is it just to check that you succeed to add an element to the ArrayList.
But on the other hand is it really possible that this Add method can return
anything else than 1.
I mean can this Add return 0 when you use an Add for the ArrayList

//Tony
 
J

Jeff Gaines

Hi!

I just wonder what is the point to return an int when you add an element
to this ArrayList ?
Is it just to check that you succeed to add an element to the ArrayList.
But on the other hand is it really possible that this Add method can
return anything else than 1.
I mean can this Add return 0 when you use an Add for the ArrayList

//Tony

The returned value is the position at which the new item has been added to
the Array List.
 
W

Willem van Rumpt

Tony said:
Hi!

I just wonder what is the point to return an int when you add an element to
this ArrayList ?
Is it just to check that you succeed to add an element to the ArrayList.
But on the other hand is it really possible that this Add method can return
anything else than 1.
I mean can this Add return 0 when you use an Add for the ArrayList

Add returns the position at which the element was added to the
ArrayList, and is implemented as part of the IList interface.

Although I don't recall ever using the return value, I suppose it could
be useful when the length of the ArrayList plays a role in your
algorithm, saving you an additional call to the count property.

So the answer would be "it exists for your convenience".
 
A

Arne Vajhøj

I just wonder what is the point to return an int when you add an element to
this ArrayList ?
Is it just to check that you succeed to add an element to the ArrayList.
But on the other hand is it really possible that this Add method can return
anything else than 1.
I mean can this Add return 0 when you use an Add for the ArrayList

http://msdn.microsoft.com/en-us/library/system.collections.arraylist.add.aspx

says:

Return Value
Type: System..::.Int32
The ArrayList index at which the value has been added.

so I would assume that it returns 0 for the first element.

It is not a status value.

The method will throw an exception if the add fails.

Arne
 

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