• List Dictionary 判断相等(根据value中的字段或属性来判断)


    List 或 Dictionary值如果是类对象,那么要在类中实现IEquatable相关接口

    public class Seat : IEquatable<Seat>
        {
            #region Field
    
            #endregion
    
            #region Property
    
            /// <summary>
            /// 名称
            /// </summary>
            public string Name { get; set; }
    
            /// <summary>
            /// IP地址
            /// </summary>
            public IPAddress IpAddr { get; set; }
    
            /// <summary>
            /// 座位ID
            /// </summary>
            public int ID { get; set; }
    
            /// <summary>
            /// 是否在线
            /// </summary>
            public bool IsOnline { get; set; }
    
            #endregion
    
            #region Constructor
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="IpAddr"></param>
            /// <param name="SeatName"></param>
            public Seat()
            {
                this.IpAddr = IPAddress.Parse("127.0.0.1");
                this.Name = "未定义";
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="IpAddr"></param>
            /// <param name="SeatName"></param>
            public Seat(IPAddress IpAddr, string SeatName)
            {
                this.IpAddr = IpAddr;
                this.Name = SeatName;
            }
    
            #endregion
    
            #region IEquatable
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="other"></param>
            /// <returns></returns>
            public bool Equals(Seat other)
            {
                if (other == null)
                    return false;
    
                if (this.Name == other.Name)
                    return true;
                else
                    return false;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public override bool Equals(Object obj)
            {
                if (obj == null)
                    return false;
    
                if (obj as Seat == null)
                    return false;
                else
                    return Equals(obj as Seat);
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public override int GetHashCode()
            {
                return this.Name.GetHashCode();
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="client1"></param>
            /// <param name="client2"></param>
            /// <returns></returns>
            public static bool operator ==(Seat client1, Seat client2)
            {
                if (((object)client1) == null || ((object)client2) == null)
                    return Object.Equals(client1, client2);
    
                return client1.Equals(client2);
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="client1"></param>
            /// <param name="client2"></param>
            /// <returns></returns>
            public static bool operator !=(Seat client1, Seat client2)
            {
                if (((object)client1) == null || ((object)client2) == null)
                    return !Object.Equals(client1, client2);
    
                return !(client1.Equals(client2));
            }
    
            #endregion
    
        }
  • 相关阅读:
    我是怎么做App token认证的
    APP和服务端-架构设计(二)
    APP和服务端-架构设计(一)
    拦截和跟踪HTTP请求的主要方法及实现
    权限控制方案之——基于URL拦截
    你真的会用Retrofit2吗?Retrofit2完全教程
    科学的解决Http Token拦截器TokenInterceptor实现
    谈谈敏捷开发(转)
    Modbus TCP 示例报文
    Modbus 通信协议详解
  • 原文地址:https://www.cnblogs.com/ChangTan/p/12938449.html
Copyright © 2020-2023  润新知