C#中怎么利用foreach遍历删除集合中的元素-创新互联

这篇文章给大家介绍C#中怎么利用foreach遍历删除集合中的元素,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联建站服务项目包括二道江网站建设、二道江网站制作、二道江网页制作以及二道江网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,二道江网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到二道江省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

方法一:采用for循环,并且从尾到头遍历

如果从头到尾正序遍历删除的话,有些符合删除条件的元素会成为漏网之鱼;

正序删除举例:

List<string> tempList = new List<string>() { "a","b","b","c" }; for (int i = 0; i < tempList.Count; i++) {  if (tempList[i] == "b")  {   tempList.Remove(tempList[i]);  } } tempList.ForEach(p => {  Console.Write(p+","); });

控制台输出结果:a,b,b,c

有两个2没有删除掉;

这是因为当i=1时,满足条件执行删除操作,会移除第一个b,接着第二个b会前移到第一个b的位置,即游标1对应的是第二个b。

接着遍历i=2,也就跳过第二个b。

用for倒序遍历删除,从尾到头

List<string> tempList = new List<string>() { "a","b","b","c" }; for (int i = tempList.Count-1; i>=0; i--) {  if (tempList[i] == "b")  {   tempList.Remove(tempList[i]);  } } tempList.ForEach(p => {  Console.Write(p+","); });

控制台输出结果:a,c,

这次删除了所有的b;

方法二:使用递归

使用递归,每次删除以后都从新foreach,就不存在这个问题了;

static void Main(string[] args) {  List<string> tempList = new List<string>() { "a","b","b","c" };  RemoveTest(tempList);  tempList.ForEach(p => {   Console.Write(p+",");  }); } static void RemoveTest(List<string> list) {  foreach (var item in list)  {   if (item == "b")   {    list.Remove(item);    RemoveTest(list);    return;   }  } }

控制台输出结果:a,c,

正确,但是每次都要封装函数,通用性不强;

方法三:通过泛型类实现IEnumerator

static void Main(string[] args) {  RemoveClass<Group> tempList = new RemoveClass<Group>();  tempList.Add(new Group() { id = 1,name="Group1" }) ;  tempList.Add(new Group() { id = 2, name = "Group2" });  tempList.Add(new Group() { id = 2, name = "Group2" });  tempList.Add(new Group() { id = 3, name = "Group3" });  foreach (Group item in tempList)  {   if (item.id==2)   {    tempList.Remove(item);   }  }  foreach (Group item in tempList)  {   Console.Write(item.id+",");  } //控制台输出结果:1,3

public class RemoveClass<T> {  RemoveClassCollection<T> collection = new RemoveClassCollection<T>();  public IEnumerator GetEnumerator()  {   return collection;  }  public void Remove(T t)  {   collection.Remove(t);  }  public void Add(T t)  {   collection.Add(t);  } } public class RemoveClassCollection<T> : IEnumerator {  List<T> list = new List<T>();  public object current = null;  Random rd = new Random();  public object Current  {   get { return current; }  }  int icout = 0;  public bool MoveNext()  {   if (icout >= list.Count)   {    return false;   }   else   {    current = list[icout];    icout++;    return true;   }  }  public void Reset()  {   icout = 0;  }  public void Add(T t)  {   list.Add(t);  }  public void Remove(T t)  {   if (list.Contains(t))   {    if (list.IndexOf(t) <= icout)    {     icout--;    }    list.Remove(t);   }  } }

关于C#中怎么利用foreach遍历删除集合中的元素就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

网站题目:C#中怎么利用foreach遍历删除集合中的元素-创新互联
浏览地址:https://www.cdcxhl.com/article12/ceidgc.html

成都网站建设公司_创新互联,为您提供网站排名定制网站云服务器小程序开发标签优化网站收录

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司