List<string> Getlist()
{
List<string> mylist;
for (bool successFlag = false; !successFlag; ) //It will definitely enter the loop once.
{
successFlag = true;
mylist = new List<string>();
for (int i = 0; i < n; i++)
{
var CDF = GetCDF(); // IEnumerable, each call of GetCDF() gives different result
if (!CDF.Any())
{
fail++;
successFlag = false;
break;
}
string item = GetNext(CDF);
mylist.Add(item);
}
}
return mylist; // Here IDE poses an error
}
I guess there is a way to recursively use Getlist() instead of do a for loop and flag retry, maybe some kind of immutable method?
i dont wanna initiate the list outside the loop because i would like to discard the list when successFlag is false;
Discard simply means when successFlag = false, then mylist.removeall. And then start everything over again as fresh, so I am asking for an approach to recursively call GetList() instead of clear the states in the method
Update
do while works!
The issue has to do with this line. If it doesn’t run you List would never be initialized.
Now if you change your code to this it should work fine.
Update :
Run untill GetCDF(); is not empty. This can cause an infinite loop if GetCDF() is always empty!!!