The code inside foreach loops may not be available if the array or collection is not initialized before execution.
List<Employee> emp; // Run when program starts, called from Program.cs private void InitialiseApplication() { emp = new List<Employee>; // Gather data for employees from... somewhere. DataAccess.GetEmployees(emp); } private void DoStuff() { foreach (var item in emp) { // Do something. } }
The above code will return a warning because "emp" is not initialized during development.
I got the same warning in my code at different stages, including inside the constructors. However, the flow of execution is not affected, because by then "emp" is initialized.
This can happen with your code. Check where and when "emp" is initialized during program flow. You may need to βjoinβ the program to achieve this, if this is not obvious in appearance.
source share