C#实现在foreach中删除集合中的元素

简介: C#实现在foreach中删除集合中的元素 1 2 3 4 5 6 7 8 List str = new List(); str.

C#实现在foreach中删除集合中的元素

1
2
3
4
5
6
7
8
List< string > str =  new  List< string >();
str.Add(  "zs" );
str.Add( "ls" );
str.Add(  "ws"  );
foreach ( string  in  str)
{
    str.Remove(s);
}

有时候我们在foreach中需要修改或者删除集合

可是这时候却报如下错误:集合已修改;可能无法执行枚举操作。

其实我们简单实现以下就可以实现这个功能

直接上代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public  class  MyClass<T>
{
     MyClassCollection<T> collection =  new  MyClassCollection<T>();
     public  IEnumerator GetEnumerator()
     {
         return  collection;
     }
     public  void  Remove(T t)
     {
         collection.Remove(t);
     }
 
     public  void  Add(T t)
     {
         collection.Add(t);
     }
}
 
public  class  MyClassCollection<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);
         }
     }
}
 
public  class  MyItem
{
     public  string  id
     {
         get ;
         set ;
     }
 
     public  int  sex
     {
         get ;
         set ;
     }
     public  string  name
     {
         get ;
         set ;
     }
 
     public  int  age
     {
         get ;
         set ;
     }
}

  然后我们直接调用一下试验下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
MyClass<MyItem> myclass =  new  MyClass<MyItem>();
//添加10条数据
Random rd =  new  Random();
for  ( int  i = 0; i < 10; i++)
{
     MyItem item =  new  MyItem();
     item.age = rd.Next(1, 80);
     item.id = rd.Next().ToString();
     item.name =  "name"  + rd.Next().ToString();
     item.sex = rd.Next(0, 1);
     myclass.Add(item);
}
 
foreach  (MyItem item  in  myclass)
{
     Console.WriteLine(item.name);
     myclass.Remove(item);
}
 
Console.Read();

  这段代码就是模拟10条数据 输出信息后直接删除

哈哈  是不是很简单呢?当然要实现修改或者其他的功能也是类似的

另外IEnumerator接口中的  public  object Current 为引用类型 所以 如果这里MyItem如果修改为基本类型的话肯定会出现拆箱装箱

如果 如果foreach中是基本类型的话就不要用foreach了 如果需要考虑性能的话。

 源代码下载:https://files.cnblogs.com/files/devgis/TestIEnumerator.rar

目录
相关文章
|
4月前
|
SQL 开发框架 .NET
C#进阶-LINQ实现对集合的增删改查
本篇演示了LINQ在日常开发中的常用操作,实现结果集的增删改查。目前LINQ支持两种语法,我会在每个案例前先用大家熟知的SQL语句表达,再在后面用C#的两种LINQ语法分别实现。LINQ语法第一次接触难免感到陌生,最好的学习方式就是在项目中多去使用,相信会有很多感悟。
41 0
|
5月前
|
存储 人工智能 C#
【Unity 3D】C#中数组、集合、栈、队列、哈希表、字典的讲解(附测试代码)
【Unity 3D】C#中数组、集合、栈、队列、哈希表、字典的讲解(附测试代码)
40 0
|
2月前
|
存储 SQL C#
C# 读取二维数组集合输出到Word预设表格
C# 读取二维数组集合输出到Word预设表格
|
2月前
|
存储 安全 C#
C#使用集合组织相关数据
C#使用集合组织相关数据
12 0
|
2月前
|
存储 安全 Java
34.C#:listT泛型集合
34.C#:listT泛型集合
18 1
|
2月前
|
存储 C#
33.c#:hashtable集合
33.c#:hashtable集合
16 1
|
2月前
|
存储 安全 Java
32.C#:ArrayList集合
32.C#:ArrayList集合
16 1
|
2月前
|
开发框架 安全 .NET
C# .NET面试系列三:集合、异常、泛型、LINQ、委托、EF!
<h2>集合、异常、泛型、LINQ、委托、EF! #### 1. IList 接口与 List 的区别是什么? IList 接口和 List 类是C#中集合的两个相关但不同的概念。下面是它们的主要区别: <b>IList 接口</b> IList 接口是C#中定义的一个泛型接口,位于 System.Collections 命名空间。它派生自 ICollection 接口,定义了一个可以通过索引访问的有序集合。 ```c# IList 接口包含一系列索引化的属性和方法,允许按索引访问、插入、移除元素等。 由于是接口,它只定义了成员的契约,而不提供具体的实现。类似于 IEnumera
168 2
|
4月前
|
存储 C# C++
C# 笔记2 - 数组、集合与与文本文件处理
C# 笔记2 - 数组、集合与与文本文件处理
48 0
|
5月前
|
存储
C#-集合小例子
C#-集合小例子
27 0