
Newbie very puzzled - no cast required??
Question arising from "C# Step by Step" about setting up an ArrayList and
then printing it....
// create an ArrayList (which lives on the heap and is an array of
// references to objects - correct?)
ArrayList countdown = new ArrayList();
countdown.Add(9); //put some values in
countdown.Add(8); //(each value is boxed - correct?)
countdown.Add(7);
...
// now print them
foreach (int number in countdown)
{
Console.WriteLine(number);
Quote:
}
The "foreach" works and I don't think it should! As I see it, "countdown"
is an array of boxed integers. So why don't I have to unbox the integers -
that is, cast "countdown" to an int - before assigning it to "number" in the
foreach statement?
If I rewrite it with a normal for loop I have to do this:
for (int i; i != countdown.Count; i++)
{
int number = (int)countdown[i]; //note: cast back to an int required
Console.WriteLine(number);
Quote:
}
It's as if the unboxing is happening implicitly in a "foreach" loop. I
thought all unboxing must be explicit. Can anyone put me out of my misery?
Thanks!
Steve
--
Steve Thackery
Web: http://www.*-*-*.com/
<antispam - remove 'not's>