enumerator

T

Tony Johansson

Hi!

I know I can use the foreach. This is just for understanding.
My question is if the rows marked with 1 below is doing the exact same thing
as those that are marked with 2. ?
Which is to say if the construction between rows marked with 1 and 2 are
identical ?

public static void Main()
{
List<string> stringList = new List<string>();
stringList.Add("opel");
stringList.Add("saab");
stringList.Add("volvo");
stringList.Add("fiat");

1 IEnumerator e = stringList.GetEnumerator();
1 while (e.MoveNext())
1 Console.WriteLine(e.Current);

2 List<string>.Enumerator e = stringList.GetEnumerator();
2 while (e.MoveNext())
2 Console.WriteLine(e.Current);
}

//Tony
 
A

Alberto Poblacion

Tony Johansson said:
I know I can use the foreach. This is just for understanding.
My question is if the rows marked with 1 below is doing the exact same
thing as those that are marked with 2. ?
Which is to say if the construction between rows marked with 1 and 2 are
identical ?

public static void Main()
{
List<string> stringList = new List<string>();
stringList.Add("opel");
stringList.Add("saab");
stringList.Add("volvo");
stringList.Add("fiat");

1 IEnumerator e = stringList.GetEnumerator();
1 while (e.MoveNext())
1 Console.WriteLine(e.Current);

2 List<string>.Enumerator e = stringList.GetEnumerator();
2 while (e.MoveNext())
2 Console.WriteLine(e.Current);
}

In the lines marked 2 you are calling directly the class that implements
the enumerator, while in the lines marked 1, you are calling the same class
through an interface. In both cases, you are invoking exactly the same code
within the compiled class, so it shouldn't make any difference.
 
K

kndg

In the lines marked 2 you are calling directly the class that implements
the enumerator, while in the lines marked 1, you are calling the same
class through an interface. In both cases, you are invoking exactly the
same code within the compiled class, so it shouldn't make any difference.

Hi Alberto,

I think Tony is playing with boxing issue...
For #1, calling e.Current will return an object type (so, cast is
required when needed).
While #2 is using the generic implementation of the enumerator, so no
casting is required.
 
M

mungflesh

Hi!

I know I can use the foreach. This is just for understanding.
My question is if the rows marked with 1 below is doing the exact same thing
as those that are marked with 2. ?
Which is to say if the construction between rows marked with 1 and 2 are
identical ?

public static void Main()
   {
      List<string> stringList = new List<string>();
      stringList.Add("opel");
      stringList.Add("saab");
      stringList.Add("volvo");
      stringList.Add("fiat");

1     IEnumerator e = stringList.GetEnumerator();
1     while (e.MoveNext())
1         Console.WriteLine(e.Current);

     2 List<string>.Enumerator e = stringList.GetEnumerator();
     2 while (e.MoveNext())
     2     Console.WriteLine(e.Current);

}

//Tony

The first section uses an enumerator whose Current property returns
object at each iteration

The second is more strongly typed, where Current returns string.
 
T

Tony Johansson

Alberto Poblacion said:
In the lines marked 2 you are calling directly the class that implements
the enumerator, while in the lines marked 1, you are calling the same
class through an interface. In both cases, you are invoking exactly the
same code within the compiled class, so it shouldn't make any difference.

I mean if I for example use the MSDN and lookup the generic class List<T>. I
can see what method I can use for example
Add, AddRange, GetEnumerator() and many more. I can also see them when using
the intellisens
in my case write stringList and a dot and there all the alternatives are
listed.

I can see this Enumerator when writing List<string> and a dot by using the
intellisense.

But now to my question if I want to find this Enumerator that I access in
statement List<string>
in the MSDN what should I lookup I can't find it if I lookup List<T> ? So
what lookup criteria should I use.

//Tony
 
K

kndg

But now to my question if I want to find this Enumerator that I access in
statement List<string>
in the MSDN what should I lookup I can't find it if I lookup List<T> ? So
what lookup criteria should I use.

List<T>.Enumerator (it is a structure)
 
P

Peter Duniho

Tony said:
Hi!

I know I can use the foreach. This is just for understanding.
My question is if the rows marked with 1 below is doing the exact same thing
as those that are marked with 2. ?

Can you be more specific about what you're asking about?

The two examples definitely do not do the exact same thing. But, the
extent to which they are different may or may not be important to you.
A couple of examples in particular:

– If you're just using Console.WriteLine(), which takes as the
argument an object reference (that being the overload selected in this
example), then the fact that you get a strongly-typed generic enumerator
in the second example isn't helpful, because the object still winds up
boxed.

– In the first example, the enumerator instance itself winds up boxed
(List<T>.Enumerator is a value type), which may or may not be important
to you.

As for your later question about finding the type in the documentation,
I'm not sure why you were unable to find the type, but it's clearly a
nested type in List<T>, so you should be able to find it easily just by
looking at the documentation for List<T>.

Pete
 
T

Tony Johansson

Peter Duniho said:
Can you be more specific about what you're asking about?

The two examples definitely do not do the exact same thing. But, the
extent to which they are different may or may not be important to you. A
couple of examples in particular:

– If you're just using Console.WriteLine(), which takes as the argument
an object reference (that being the overload selected in this example),
then the fact that you get a strongly-typed generic enumerator in the
second example isn't helpful, because the object still winds up boxed.

– In the first example, the enumerator instance itself winds up boxed
(List<T>.Enumerator is a value type), which may or may not be important to
you.

As for your later question about finding the type in the documentation,
I'm not sure why you were unable to find the type, but it's clearly a
nested type in List<T>, so you should be able to find it easily just by
looking at the documentation for List<T>.

Pete

How can these two examples cause boxing when I use generics with T as string
???
One of the reasons to use generics is just that no boxing/unboxing need to
be made.
One more question the type that is send to WriteLine is string isn't it so
here is just the ToString used for String because it is overridden ???

Now the last question if I use MSDN and lookup List<T> this Enumerator is
not there. You can look for youself ??

//Tony
 
P

Peter Duniho

Tony said:
How can these two examples cause boxing when I use generics with T as string
???

Well, the first causes boxing simply by getting the enumerator.
IEnumerable is a reference type (interface) but the implementation is a
value type. Thus, it needs to be boxed.

As for the second example, it's because you are calling
Console.WriteLine(). Even though the enumerator itself doesn't need
boxing, every object enumerated does.
One of the reasons to use generics is just that no boxing/unboxing need to
be made.

Console.WriteLine() isn't generic. How is it going to avoid boxing?
One more question the type that is send to WriteLine is string isn't it so
here is just the ToString used for String because it is overridden ???

You didn't call ToString(). You are passing the object and letting the
WriteLine() method call ToString(). So the object has to be boxed.
Now the last question if I use MSDN and lookup List<T> this Enumerator is
not there.

Yes, it is.
You can look for youself ??

You can too:
http://msdn.microsoft.com/en-us/library/x854yt9s.aspx

Pete
 

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