Contains,Exists,Any,Count 比较是否存在某个元素

时间:2023-03-01 08:06:31
private static void Main(string[] args)
{
int count = ;
Console.WriteLine("判断是否存在某个元素 :");
Console.WriteLine("\t值类型比较 :");
Contains_Exists_Any_Test(count);
Console.WriteLine();
Console.WriteLine("\t引用类型比较 :");
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 = ; 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) > );
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 = ; 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) > );
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;
}
}

Contains,Exists,Any,Count 比较是否存在某个元素

结论:

值类型 : 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;
}

结果:

Contains,Exists,Any,Count 比较是否存在某个元素

结论跟上述一样.