the type of the variable declared with var is determined at compile time. Foreach assigns values to the var item variable at run time. But isn’t this contrary to the working logic of the var keyword? Because the type of the variable is not yet known.
ArrayList myList = new ArrayList();
// Öğeleri ekleme
myList.Add("Apple");
myList.Add(123);
myList.Add(45.67);
myList.Add(true);
// Listeyi yazdırma
foreach (var item in myList)
{
Console.WriteLine(item);
}
>Solution :
Each item returned by the ArrayList object’s enumerator is of type object.
var is still determined at the compile time.