how to remove a item from System.Collections.Generic.List in a loo

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Hi zhanglr,

Make a new list of removing objects and remove them after the loop (you cannot
modify the list of foreach during its execution).
But, it may be better to make the list of passed objects instead of deleted.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



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

First of all, thank you for your reply.

I just wonder whether there is any better way. E.g. in C++ we are able to
call std::remove_if or list::erase (the return value is iterator so I am able
to continue.) I think maybe MS has provided the samilar functions. I just
haven't found it yet.

At the end, thanks for your reply again.

Best Regards
Michael zhang
 
Hi zhanglr,

Because you will have two arrays: one as a source (as immutable) and second
is filtered. Safe way.

And yes, MS provides similar functions: you can also use e.g. list.RemoveAll(delegate(int
value) { return <some_case_to_return_bool>; }) to clear particular values
(see also other .Remove<some> functions of list)

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



z> Hi,
z>
z> First of all, thank you for your reply.
z>
z> I just wonder whether there is any better way. E.g. in C++ we are
z> able to call std::remove_if or list::erase (the return value is
z> iterator so I am able to continue.) I think maybe MS has provided the
z> samilar functions. I just haven't found it yet.
z>
z> At the end, thanks for your reply again.
z>
z> Best Regards
z> Michael zhang
z> "Alex Meleta" wrote:
z>
Hi zhanglr,

Make a new list of removing objects and remove them after the loop
(you cannot modify the list of foreach during its execution). But, it
may be better to make the list of passed objects instead of deleted.

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

Thank you so much.

Best Regards,
Michael zhang

Alex Meleta said:
Hi zhanglr,

Because you will have two arrays: one as a source (as immutable) and second
is filtered. Safe way.

And yes, MS provides similar functions: you can also use e.g. list.RemoveAll(delegate(int
value) { return <some_case_to_return_bool>; }) to clear particular values
(see also other .Remove<some> functions of list)

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



z> Hi,
z>
z> First of all, thank you for your reply.
z>
z> I just wonder whether there is any better way. E.g. in C++ we are
z> able to call std::remove_if or list::erase (the return value is
z> iterator so I am able to continue.) I think maybe MS has provided the
z> samilar functions. I just haven't found it yet.
z>
z> At the end, thanks for your reply again.
z>
z> Best Regards
z> Michael zhang
z> "Alex Meleta" wrote:
z>
Hi zhanglr,

Make a new list of removing objects and remove them after the loop
(you cannot modify the list of foreach during its execution). But, it
may be better to make the list of passed objects instead of deleted.

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