what is the point when the ArrayList return an int

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
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
 
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.
 
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".
 
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
 
Back
Top