Newbie very puzzled - no cast required?? 
Author Message
 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>



Thu, 14 Oct 2004 03:58:08 GMT  
 Newbie very puzzled - no cast required??
 foreach (int number in countdown)

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 -

BUT, look back at this part "int number" ... you are effectively doing your
cast right there.  It's hidden in plain sight.  You don't see the cast, but
you must realize that you couldn't even use "number" if it hadn't already
been cast for you.

Chris R.


Quote:
> 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);
> }

> 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);
> }

> 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://home.clara.net/steve.thackery/

> <antispam - remove 'not's>



Thu, 14 Oct 2004 04:14:44 GMT  
 Newbie very puzzled - no cast required??
... continued,

More specifically, to understand this, if you try:
    foreach ( double number in countdown)
You will get the following error:  "An unhandled exception of type
'System.InvalidCastException' occurred in  ..."  at runtime.  As you can
presume, the only way you can get that error is when a cast is being done,
albeit in the background.  The "foreach" statement provides this for you, to
simplify your code.

Chris R.


Quote:
> 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);
> }

> 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);
> }

> 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://home.clara.net/steve.thackery/

> <antispam - remove 'not's>



Thu, 14 Oct 2004 04:22:27 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Puzzle on type casting an integer

2. Help with structure element casting required

3. casting for varargs required?

4. Why is casting required here?

5. Newbie Scanf Puzzles

6. puzzled newbie

7. Newbie has puzzling question about static class members

8. Help required for newbie

9. Newbie requires source etc

10. Newbie - help required with web integration

11. Newbie requires mouse help

12. DOS newbie help required

 

 
Powered by phpBB® Forum Software