码迷,mamicode.com
首页 > Windows程序 > 详细

C#编程(三十七)----------结构比较

时间:2017-03-22 10:34:40      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:throw   sys   return   task   style   ext   显示   强制   eric   

结构比较

数组和元组都实现接口IStructuralEquatableIStructuralComparable.这两个接口不仅可以比较引用,还可以比较内容.这些接口都是显示实现的,所以在使用时需要把数组和元组强制转换为这个接口.IStructuralEquatable接口用于比较两个元组或数组是否有相同的内容,IStructuralComparable接口用于给元组或数组排序.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace 结构比较

{

    class Program

    {

        static void Main(string[] args)

        {

           //创建两个Person项的数组.

            //比较运算符!=返回true

            //因为这其实是两个变量p1p2引用的两个不同数组.

            //因为array类没有重写带一个参数的Equals()放大,所以用"=="运算符

            //比较引用会得到相同的结构,即这两个变量不相同

            Person zhangsan = new Person { FirstName = "zhang", LastName = "san" };

            Person[] p1 = {

                              new Person

                                {

                                    FirstName="lisi"

                                } ,

            zhangsan

                          };

            Person[] p2 = {

                              new Person

                                {

                                    FirstName="lisi"

                                } ,

            zhangsan

                          };

            if (p1!=p2)

            {

                Console.WriteLine("not the same reference"); ;

            }

            Console.ReadKey();

        }

    }

    public class Person : IEquatable<Person>

    {

        public int ID { get; set; }

        public string FirstName { set; get; }

        public string LastName { set; get; }

 

        public override string ToString()

        {

            return string.Format("{0},{1}{2}", ID, FirstName, LastName); ;

        }

        public override bool Equals(object obj)

        {

            if (obj == null)

            {

                throw new ArgumentNullException("obj");

            }

            return Equals(obj as Person);

        }

        public override int GetHashCode()

        {

            return ID.GetHashCode();

        }

        public bool Equals(Person other)

        {

            if (other == null)

            {

                throw new ArgumentNullException("other");

            }

            return this.ID == other.ID && this.FirstName == other.FirstName && this.LastName == other.LastName;

        }

    }

}

 

使用实现IEquatable接口的Person.IEquatable接口定义了一个强类型化的Equals()方法,用来比较FirstNameLastName属性的值.

 

下面看看如何对元组执行相同的操作.这里创建了两个内容相同的元组实例:

 var t1 = Tuple.Create<string, int>("zhangsan", 19);

            var t2 = Tuple.Create<string, int>("zhangsan", 19);

            //因为t1t2引用了两个不同的对象,所以比较运算符"!="返回true

            if (t1 != t2)

            {

                Console.WriteLine("not the same reference to the tuple");

            }

            //这会调用object.equals()方法比较元组的每一项,每一项都返回true

            if (t1.Equals(t2))

            {

                Console.WriteLine("the same reference to the tuple");

            }

Tuple<>类提供了两个Equals()方法:一个重写了object基类中的Equals()方法,并把object作为参数,第二个由IStructyralEqualityComparer接口定义,并把objectIequalityComparer作为参数.

 

还可以使用类TupleCOmparer创建一个自定义的UequalityComparer,这个类实现了IEqualityComparer接口的两个方法Equals()GetHashCode():

 public class TupleComparer : IEqualityComparer

    {

        public bool Equals(object x, object y)

        {

            return x.Equals(y);

        }

 

        public int GetHashCode(object obj)

        {

            return obj.GetHashCode();

        }

}

实现IEqualityCOmparer接口的Equals()方法需要new修饰符或者隐式实现的接口,因为基类object也定义了带两个参数的静态的Equals()方法.

 

使用TupleComparer,Tuple<T1,T2>类的Equals()方法传递一个新实例.Tuple类的Equals(0方法为要比较的每一项调用TupleComparerEquals()方法.所以,对于Tuple<T1,T2>,要调用两次TupleCOmparer,以检查所有项是否相等.

C#编程(三十七)----------结构比较

标签:throw   sys   return   task   style   ext   显示   强制   eric   

原文地址:http://www.cnblogs.com/android-blogs/p/6598523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!