C# 列表<&燃气轮机;并包含使用asp.net 2.0比较“排除”和“包含”的方案

C# 列表<&燃气轮机;并包含使用asp.net 2.0比较“排除”和“包含”的方案,c#,C#,我有两个列表,其中包含一个类似于标记的列表的对象。我需要查找排除和包含以执行更新操作。由于它们包含一个标记对象,如何查找两者之间的差异。您是否可以在列表中使用带有标记对象的Contains函数?需要一些帮助 我使用的是asp.NET2.0,所以如果你能帮我用那种语言,请帮我 List<Tag> tag1 = new List<Tag>() tag1.Add(new Tag("Apples")); tag1.Add(new Tag("Oranges")); ta

我有两个列表,其中包含一个类似于标记的列表的对象。我需要查找排除和包含以执行更新操作。由于它们包含一个标记对象,如何查找两者之间的差异。您是否可以在列表中使用带有标记对象的Contains函数?需要一些帮助

我使用的是asp.NET2.0,所以如果你能帮我用那种语言,请帮我

List<Tag> tag1 = new List<Tag>()  
tag1.Add(new Tag("Apples"));  
tag1.Add(new Tag("Oranges"));  
tag1.Add(new Tag("Pears"));  

List<Tag> tag2 = new List<Tag>()  
tag2.Add(new Tag("Apples"));  
tag2.Add(new Tag("Bananas"));  

txtExcluded.Text = list1;  
txtIncluded.Text = list2  
List tag1=新列表()
tag1.添加(新标签(“苹果”));
tag1.添加(新标签(“橙子”);
添加(新标签(“梨”);
List tag2=新列表()
添加(新标签(“苹果”);
添加(新标签(“香蕉”);
txtExcluded.Text=list1;
txtIncluded.Text=list2
您可以实现:

公共类标记:IEquatable
{
公共标记(字符串文本)
{
文本=文本;
}
公共字符串文本{get;set;}
公共布尔等于(标记其他)
{
返回other!=null&&string.Equals(Text,other.Text);
}
公共覆盖布尔等于(对象对象对象)
{
返回等于(obj作为标记);
}
公共覆盖int GetHashCode()
{
返回(文本??string.Empty).GetHashCode();
}
}
现在,Contains方法的工作原理如下:

var list = new List<Tag>(new[]
{
    new Tag("Apples"),
    new Tag("Oranges"),
    new Tag("Pears"),
});

var tag = new Tag("Pears");
bool isContains = list.Contains(tag); // returns true
var list=新列表(新[]
{
新标签(“苹果”),
新标签(“橙子”),
新标签(“梨”),
});
var标签=新标签(“梨”);
bool isContains=list.Contains(标记);//返回true
您可以实现:

公共类标记:IEquatable
{
公共标记(字符串文本)
{
文本=文本;
}
公共字符串文本{get;set;}
公共布尔等于(标记其他)
{
返回other!=null&&string.Equals(Text,other.Text);
}
公共覆盖布尔等于(对象对象对象)
{
返回等于(obj作为标记);
}
公共覆盖int GetHashCode()
{
返回(文本??string.Empty).GetHashCode();
}
}
现在,Contains方法的工作原理如下:

var list = new List<Tag>(new[]
{
    new Tag("Apples"),
    new Tag("Oranges"),
    new Tag("Pears"),
});

var tag = new Tag("Pears");
bool isContains = list.Contains(tag); // returns true
var list=新列表(新[]
{
新标签(“苹果”),
新标签(“橙子”),
新标签(“梨”),
});
var标签=新标签(“梨”);
bool isContains=list.Contains(标记);//返回true

是.Net 2.0的IEquatable部分如果实现
IEquatable
你真的应该重写
Equals
GetHashCode
-否则混乱随之而来。@kirkdm,
IEquatable
在.Net 4、3.5、3.0中受支持,如果实现
IEquatable
,你真的应该覆盖
Equals
GetHashCode
-否则混乱随之而来。@kirkdm,
IEquatable
在.Net 4、3.5、3.0、2.0中受支持