C# Contains用法
答案:3 悬赏:20 手机版
解决时间 2021-02-25 02:07
- 提问者网友:川水往事
- 2021-02-24 12:02
Contains()这个方法,对用引用类型,比较的是值,还是地址。
最佳答案
- 五星知识达人网友:行路难
- 2021-02-24 12:43
根据你的容器中的内容来定。等下贴代码示例
using System;
using System.Collections.Generic;
namespace Test_zhidao
{
class A{
public int a;
#region Equals and GetHashCode implementation
public override bool Equals(object obj)
{
A other = obj as A;
if (other == null)
return false;
return this.a == other.a;
}
public override int GetHashCode()
{
int hashCode = 0;
unchecked {
hashCode += 1000000007 * a.GetHashCode();
}
return hashCode;
}
public static bool operator ==(A lhs, A rhs)
{
if (ReferenceEquals(lhs, rhs))
return true;
if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null))
return false;
return lhs.Equals(rhs);
}
public static bool operator !=(A lhs, A rhs)
{
return !(lhs == rhs);
}
#endregion
}
class B{
public int b;
}
class Program
{
public static void Main(String[] args)
{
List<A> list_a = new List<A>();
A obja1 = new A();
obja1.a = 100;
A obja2 = new A();
obja2.a = 100;
list_a.Add(obja1);
Console.WriteLine(list_a.Contains(obja2));//True
List<B> list_b = new List<B>();
B objb1 = new B();
objb1.b = 100;
B objb2 = new B();
objb1.b = 100;
list_b.Add(objb1);
Console.WriteLine(list_b.Contains(objb2));//False
Console.ReadKey(true);
}
}
}也就是说,这个是根据类的Equals方法来定的,而object 顶级类,是以地址比较为准的,你的子类如果不重载,那么就比较地址,如果你重载了,就按照你指定的方式来比较。
using System;
using System.Collections.Generic;
namespace Test_zhidao
{
class A{
public int a;
#region Equals and GetHashCode implementation
public override bool Equals(object obj)
{
A other = obj as A;
if (other == null)
return false;
return this.a == other.a;
}
public override int GetHashCode()
{
int hashCode = 0;
unchecked {
hashCode += 1000000007 * a.GetHashCode();
}
return hashCode;
}
public static bool operator ==(A lhs, A rhs)
{
if (ReferenceEquals(lhs, rhs))
return true;
if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null))
return false;
return lhs.Equals(rhs);
}
public static bool operator !=(A lhs, A rhs)
{
return !(lhs == rhs);
}
#endregion
}
class B{
public int b;
}
class Program
{
public static void Main(String[] args)
{
List<A> list_a = new List<A>();
A obja1 = new A();
obja1.a = 100;
A obja2 = new A();
obja2.a = 100;
list_a.Add(obja1);
Console.WriteLine(list_a.Contains(obja2));//True
List<B> list_b = new List<B>();
B objb1 = new B();
objb1.b = 100;
B objb2 = new B();
objb1.b = 100;
list_b.Add(objb1);
Console.WriteLine(list_b.Contains(objb2));//False
Console.ReadKey(true);
}
}
}也就是说,这个是根据类的Equals方法来定的,而object 顶级类,是以地址比较为准的,你的子类如果不重载,那么就比较地址,如果你重载了,就按照你指定的方式来比较。
全部回答
- 1楼网友:摆渡翁
- 2021-02-24 14:00
Contains方法的源码如下,比较的是值
public bool Contains(string value)
{
return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}
- 2楼网友:爱难随人意
- 2021-02-24 13:07
根据你的容器中的内容来定。等下贴代码示例
using system; using system.collections.generic; namespace test_zhidao { class a{ public int a; #region equals and gethashcode implementation public override bool equals(object obj) { a other = obj as a; if (other == null) return false; return this.a == other.a; } public override int gethashcode() { int hashcode = 0; unchecked { hashcode += 1000000007 * a.gethashcode(); } return hashcode; } public static bool operator ==(a lhs, a rhs) { if (referenceequals(lhs, rhs)) return true; if (referenceequals(lhs, null) || referenceequals(rhs, null)) return false; return lhs.equals(rhs); } public static bool operator !=(a lhs, a rhs) { return !(lhs == rhs); } #endregion } class b{ public int b; } class program { public static void main(string[] args) { list<a> list_a = new list<a>(); a obja1 = new a(); obja1.a = 100; a obja2 = new a(); obja2.a = 100; list_a.add(obja1); console.writeline(list_a.contains(obja2));//true list<b> list_b = new list<b>(); b objb1 = new b(); objb1.b = 100; b objb2 = new b(); objb1.b = 100; list_b.add(objb1); console.writeline(list_b.contains(objb2));//false console.readkey(true); } } }也就是说,这个是根据类的equals方法来定的,而object 顶级类,是以地址比较为准的,你的子类如果不重载,那么就比较地址,如果你重载了,就按照你指定的方式来比较。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯