Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using var keyword with foreach

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Each item returned by the ArrayList object’s enumerator is of type object.
var is still determined at the compile time.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading