In c# only the member variables gets assigned with the default values, all the local vaiabiable needs to be assigned explictely.
This is basic rule that you can’t access a variable which is not assigned any value. In you case compiler is not sure that if
foreach
loop will run or not and same with the
if
condition within for loop, thats why you are getting the error.
For example
bool success;
if(something)
{
success= false;
}
bool success;
If(something)
{
success=false;
}
else
{
success=true;
}