小编dev*_*zim的帖子

为什么要实现IEquatable <T>接口

我一直在阅读文章并理解一定程度的接口,如果我想改正我自己的自定义Equals方法,似乎我可以在不实现IEquatable接口的情况下做到这一点.一个例子.

using System;
using System.Collections;
using System.ComponentModel;

namespace ProviderJSONConverter.Data.Components
{
    public class Address : IEquatable<Address>
    {
        public string address { get; set; }

        [DefaultValue("")]
        public string address_2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }

        public bool Equals(Address other)
        {
            if (Object.ReferenceEquals(other, null)) return false;
            if (Object.ReferenceEquals(this, other)) return true;
            return (this.address.Equals(other.address)
                && this.address_2.Equals(other.address_2)
                && this.city.Equals(other.city)
                && this.state.Equals(other.state)
                && this.zip.Equals(other.zip));
        }
    }
 } …
Run Code Online (Sandbox Code Playgroud)

c# asp.net polymorphism interface

3
推荐指数
2
解决办法
2540
查看次数

标签 统计

asp.net ×1

c# ×1

interface ×1

polymorphism ×1