Reputation: 180
I have a question about ArrayList (C#) and I think I know the answer but need confirmation. I would like a solid foundation of knowledge.
So here is my question: I was looking at example of ArrayLists and came across this line of code:
for (int i=1; i<=items.Count; i++)
{Console.WriteLine("{0}. {1}", i, (String)items[i-1]);}
Here is what I think I know what's going on.
Here is what I am unsure about. 1. (String) << this I believe is casting the value in the current ArrayList index to a String type? 2. What is the type of an ArrayList item? Is it just an object type?
The concept is a bit foggy to me and I am just looking for a bit of clarity. Thanks for any help you can spare.
Upvotes: 0
Views: 394
Reputation: 2694
I'll try to give strict explanation
- a for loop is executed for each item in the ArrayList.
Loop continues to execute as long as condition i<=items.Count remains true.
- the code executed is a method call to the Console's WriteLine method with a formal parameter.
Formal parameter is the one, that you declare in the signature of the method. The parameter that you pass to the method is called actual paramter.
- {0} will be replaced with the current value of i in the for loop and {1} will be replaced 4. with the value in the ArrayList index of [i - 1].
{0} and {1} will be replaced with a value of ToString() call of parameters that are passed at the second and third position of the Console.Writeline
the reason for [i - 1] is because the index of an ArrayList begins at 0 and not 1.
Right
Here is what I am unsure about. 1. (String) << this I believe is casting the value in the current ArrayList index to a String type?
You are right. This is casting. Also, this cast is not necessary (Console.Writeline calls ToString() on its arguments). It will cause InvalidCastException, if the item in the arraylist is of type other than String.
- What is the type of an ArrayList item? Is it just an object type?
Right
Upvotes: 0
Reputation: 838796
Arrays and lists are 0-indexed in C#. This means that the first item is at index 0
and the last item is at index items.Count - 1
. I think your code would be clearer if your for loop started from 0
:
for (int i = 0; i < items.Count; i++)
{
Console.WriteLine("{0}. {1}", i + 1, (string)items[i]);
}
The indexer of ArrayList
has a return type of object
, so if your list contains strings you will normally want to cast to string when you fetch an object from the ArrayList
. However in this specific case there's no need to perform the cast because the WriteLine
overload you are calling has the signature void WriteLine(string, object, object)
. It's fine to just pass an object
to this method. Internally the WriteLine
method will call ToString
on your object.
You should also consider using the generic List<T>
class instead of ArrayList
. The ArrayList
class was useful when .NET was first released, but new code in .NET 2.0 or above should prefer to use List<T>
.
Upvotes: 3
Reputation: 1759
yes, it's a cast to string of the individual element in the arraylist reference by the i-1. this is assuming each element in the arraylist is a string. if any are not, an invalidcastexception will be thrown. if any elements are null, a nullreferenceexception will be thrown. i think you are asking the question because of uncertainty about the precedence of operations. without knowledge of precedence, you can be confused about whether you are casting an element in the arraylist to string or the arraylist itself (which would throw an invalidcastexception, were you to try to do that). as mentioned before, in this case it is unnecessary because tostring will be called by console.writeline.
yes, arraylist is a list of objects. as mentioned before, generics provide the ability to have lists of more specifically types items.
also worth nothing, starting the loop with an index of 1 is inconvenience. life is usually better if you start with 0 and use < for the loop continue condition rather than <=. better still if you don't need to use the index itself is to use a foreach loop.
Upvotes: 0
Reputation: 112612
Your analysis is correct.
The .NET frameworks 1.0 and 1.1 did not have generics. Because of this ArrayList
dating from this time has items of object
type. Since .NET 2.0 the generic List<T>
is available, which made the old ArrayList
obsolete in most cases.
Define your list as
var items = new List<string>();
Then you can drop the casting.
Upvotes: 0
Reputation: 2301
Unlike a List where we define the Type of the objects in the List, ArrayLists are not generics.
It turns out that internally the ArrayList uses an array of type object
to store its values. In our code, we must treat its values as if we declared a List<object>
.
So, since the type is not defined past object
, we must cast our values if we want to define them as a subclass of object
(i.e. String
in this case) because otherwise the compiler will treat it as the type object
and we will not be able to perform String
operations.
Note: In this case the cast may not be necessary because the object's ToString()
method will implicitly be called, but if we want to perform other String
operations (i.e. Substring
) we will in fact need the cast.
Upvotes: 0
Reputation: 66657
(String) is casting, that is correct. As per this casting, yes ArrayList should contain any object of type String, otherwise there will be classcastexception.
Type of arraylist should be object of String.
Upvotes: 0
Reputation: 755189
The concept is a bit foggy because this loop is being done in a non-standard way. The idiomatic way of looping through an array / list in C# is to do so with a 0 starting index
for (int i = 0; i < items.Conut; i++) {
String current = (String)items[i];
Console.WriteLine("{0}. {1}", i + 1, current);
}
It seems a bit untrue for the code to be printing out the index of the item + 1 though (which is what the code does). The original author of this code seems to be more familiar with arrays which are indexed starting at 1 vs. the CLR / C# way of starting at 0.
Upvotes: 2