G
Guest
Hi,
Following code does not work. (it will crash.)-- I think the reason is that
I cannot modify list (call remove) in foreach. But I don't know what I can
do.
-----------------------------------------------------------------------
List<string> arstr = new List<string>();
//init data
for (int i = 0; i < 30; ++i)
{
arstr.Add(i.ToString());
}
//go through items one by one
//and remove item start with 1 e.g. 1, 10,11...,100...
foreach (string str in arstr)
{
if (str.Length > 0 && str[0] == '1')
{
arstr.Remove(str);
}
}
//print the result
foreach (string str in arstr)
{
Console.WriteLine(str);
}
Best Regards,
Michael zhang
Following code does not work. (it will crash.)-- I think the reason is that
I cannot modify list (call remove) in foreach. But I don't know what I can
do.
-----------------------------------------------------------------------
List<string> arstr = new List<string>();
//init data
for (int i = 0; i < 30; ++i)
{
arstr.Add(i.ToString());
}
//go through items one by one
//and remove item start with 1 e.g. 1, 10,11...,100...
foreach (string str in arstr)
{
if (str.Length > 0 && str[0] == '1')
{
arstr.Remove(str);
}
}
//print the result
foreach (string str in arstr)
{
Console.WriteLine(str);
}
Best Regards,
Michael zhang