泛型list的删除,如何不根据索引来删除?

seesee 2013-04-15 02:25:33
1 泛型类
public class DataItem<Type1, Type2>
{
public Type1 Text
{
get;
set;
}

public Type2 Value
{
get;
set;
}
}
2 list
List<DataItem<string, int>> list = new List<DataItem<string, int>>();//定义

list.Add(new DataItem<string, int> { Text = "采购", Value = 1 });//添加

list.Remove(new DataItem<string, int> { Text = "采购", Value = 1 });
// 无法删除
该怎么样删除呢?不用list.RemoveAt[0],不用索引
...全文
247 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
seesee 2013-04-15
  • 打赏
  • 举报
回复
引用 11 楼 NianHui 的回复:
反汇编工具 Reflector7 或者 ILSPY
谢谢!
#blackheart 2013-04-15
  • 打赏
  • 举报
回复
反汇编工具 Reflector7 或者 ILSPY
seesee 2013-04-15
  • 打赏
  • 举报
回复
引用 9 楼 NianHui 的回复:
List<T>的Remove源码 public bool Remove(T item) { int num = this.IndexOf(item); if (num >= 0) { this.RemoveAt(num); return true; } return false; } 它是先找到索引,然后再按索引删除的。
那您是怎找到这些方法的实现的?
#blackheart 2013-04-15
  • 打赏
  • 举报
回复
List<T>的Remove源码 public bool Remove(T item) { int num = this.IndexOf(item); if (num >= 0) { this.RemoveAt(num); return true; } return false; } 它是先找到索引,然后再按索引删除的。 所以应该看IndexOf方法; public int IndexOf(T item) { return Array.IndexOf<T>(this._items, item, 0, this._size); } 进一步Array.IndexOf<T> public static int IndexOf<T>(T[] array, T value, int startIndex, int count) { if (array == null) { throw new ArgumentNullException("array"); } if (startIndex < 0 || startIndex > array.Length) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (count < 0 || count > array.Length - startIndex) { throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); } return EqualityComparer<T>.Default.IndexOf(array, value, startIndex, count); } 再进一步EqualityComparer<T>.Default.IndexOf internal virtual int IndexOf(T[] array, T value, int startIndex, int count) { int num = startIndex + count; for (int i = startIndex; i < num; i++) { if (this.Equals(array[i], value)) { return i; } } return -1; } 再深入没找到了,这个this.Equals是个抽象方法。不过内部肯定是调用类型T的Equals(没重写则调用Object的,Object的默认Equals比较的是引用,而不是具体的属性值,所以你第一个添加的那个对象和你移除的那个不是同一引用,故返回false,就没找到元素,也就没有移除)来比较相等。重写之后就会按照比较属性值而不是引用了. 再优化一下 public override bool Equals(object obj) { DataItem<Type1, Type2> other = obj as DataItem<Type1, Type2>; if (other == null) { return false; } if (Object.ReferenceEquals(this, other)) { return true; } return Object.Equals(this.Text, other.Text) && Object.Equals(this.Value, other.Value); }
seesee 2013-04-15
  • 打赏
  • 举报
回复
引用 7 楼 NianHui 的回复:
能不能加上点语言描述呢? ???描述代码?还是原理
原理! 谢谢大牛!
#blackheart 2013-04-15
  • 打赏
  • 举报
回复
引用 6 楼 justalways 的回复:
引用 4 楼 NianHui 的回复:C# code?1234567891011121314151617181920212223 public class DataItem<Type1, Type2> { publi…… 能不能加上点语言描述呢?
???描述代码?还是原理
seesee 2013-04-15
  • 打赏
  • 举报
回复
引用 4 楼 NianHui 的回复:
C# code?1234567891011121314151617181920212223 public class DataItem<Type1, Type2> { publi……
能不能加上点语言描述呢?
seesee 2013-04-15
  • 打赏
  • 举报
回复
哎!咋差距这么大呢。 [quote=引用 4 楼 NianHui 的回复:] 谢谢大牛!
#blackheart 2013-04-15
  • 打赏
  • 举报
回复
引用 3 楼 justalways 的回复:
我用lambda实现了list.RemoveAll(). 但是我还是想知道list.Remove()怎么用,而为啥不能删除?

    public class DataItem<Type1, Type2>
    {
        public Type1 Text
        {
            get;
            set;
        }

        public Type2 Value
        {
            get;
            set;
        }
        public override bool Equals(object obj)
        {
            DataItem<Type1, Type2> other = obj as DataItem<Type1, Type2>;
            if (other == null)
            {
                return false;
            }
            return Object.Equals(this.Text, other.Text) && Object.Equals(this.Value, other.Value);
        }
    }
重写下Equals
seesee 2013-04-15
  • 打赏
  • 举报
回复
我用lambda实现了list.RemoveAll(). 但是我还是想知道list.Remove()怎么用,而为啥不能删除?
#blackheart 2013-04-15
  • 打赏
  • 举报
回复
list.RemoveAll((item) => { return item.Value == 1 && item.Text == "采购"; });
iceMung 2013-04-15
  • 打赏
  • 举报
回复
list.RemoveAll(表达式)

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧