private static void Main(string[] args) { int count = 100000000; Console.WriteLine("判断是否存在某个元素 :"); Console.WriteLine(" 值类型比较 :"); Contains_Exists_Any_Test(count); Console.WriteLine(); Console.WriteLine(" 引用类型比较 :"); Contains_Exists_Any_Test_Complex(count); Console.ReadKey(); } /// <summary> /// 值类型比较 /// </summary> /// <param name="num"></param> public static void Contains_Exists_Any_Test(int num) { List<int> list = new List<int>(); int N = num; for (int i = 0; i < N; i++) { list.Add(i); } System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Console.WriteLine(list.Contains(N)); sw.Stop(); Console.WriteLine("Contains:" + sw.Elapsed.ToString()); sw.Reset(); sw.Start(); Console.WriteLine(list.Exists(i => i == N)); sw.Stop(); Console.WriteLine("Exists:" + sw.Elapsed.ToString()); sw.Reset(); sw.Start(); Console.WriteLine(list.Any(i => i == N)); sw.Stop(); Console.WriteLine("Any:" + sw.Elapsed.ToString()); sw.Reset(); sw.Start(); Console.WriteLine(list.Count(i => i == N) > 0); sw.Stop(); Console.WriteLine("Count:" + sw.Elapsed.ToString()); } /// <summary> /// 引用类型比较 /// </summary> /// <param name="num"></param> public static void Contains_Exists_Any_Test_Complex(int num) { List<Person> list = new List<Person>(); Person person = new Person { Id = num }; int N = num; for (int i = 0; i < N; i++) { list.Add(new Person { Id = i }); } System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Console.WriteLine(list.Contains(person)); sw.Stop(); Console.WriteLine("Contains:" + sw.Elapsed.ToString()); sw.Reset(); sw.Start(); Console.WriteLine(list.Exists(i => i.Id == person.Id)); sw.Stop(); Console.WriteLine("Exists:" + sw.Elapsed.ToString()); sw.Reset(); sw.Start(); Console.WriteLine(list.Any(i => i.Id == person.Id)); sw.Stop(); Console.WriteLine("Any:" + sw.Elapsed.ToString()); sw.Reset(); sw.Start(); Console.WriteLine(list.Count(i => i.Id == person.Id) > 0); sw.Stop(); Console.WriteLine("Count:" + sw.Elapsed.ToString()); }
public class Person : IEquatable<Person> { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public override bool Equals(object obj) { //if (ReferenceEquals(null, obj)) //{ // return false; //} //if (ReferenceEquals(this, obj)) //{ // return true; //} //if (obj.GetType() != GetType()) //{ // return false; //} return Equals((Person)obj); } public bool Equals(Person other) { //if (ReferenceEquals(null, other)) //{ // return false; //} //if (ReferenceEquals(this, other)) //{ // return true; //} return Id == other.Id; } public override int GetHashCode() { return Id; } }
结论:
值类型 : Contais > Exists > Any (Count)
引用类型 : Exists > Contains > Any (Count)
将对象需要比较的属性增加为3个:
list.Exists(i => i.Id == person.Id && i.Age == person.Age && i.Name == person.Name)
public bool Equals(Person other) { //if (ReferenceEquals(null, other)) //{ // return false; //} //if (ReferenceEquals(this, other)) //{ // return true; //} return Id == other.Id && Age == other.Age && Name == other.Name; }
结果:
结论跟上述一样.