What is the thinking that foreach applies to a variable type?

It looks like we could just have a standard statically verified job. I'm not sure I see this as an advantage. What am I missing?

+3
source share
2 answers

The problem here is that it foreachwas developed before the generic, which did not exist before .NET 2.0. Since .NET 1.1, if you used a collection class, the IEnumeratorinterface property Currentalways returns System.Object.

Having a foreach throw, you can write:

foreach(string item in collection)

Instead of explicitly writing:

foreach(object temp in collection)
{
     string item = (string)temp;

Of course, with .NET 2 this is no longer a problem.

+3
source

foreach IEnumerable, , ArrayList. , .NET ArrayList , , , ​​.

, , , - , , , , . , ArrayList.

+1

All Articles