.Net 相等性的测试题目,看你基础牢不牢

本文探讨了C#中不同集合类型(如数组、列表、集合及字典)对自定义类实例进行相等性测试的方法。通过逐步完善自定义类People的实现,展示了如何正确实现Equals方法及其相关操作符以确保一致的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有这么一个简单类People:

    class People
    {
        
public int Id { getset; }
        
public string Name { getset; }

        
public override string ToString()
        {
            
return string.Format("Id: {0}; Name: {1}", Id, Name);
        }
    }

请看以下代码,给出b0~b6的值(b6的相关代码不熟悉的话,可以只给出b0~b5的值):

    private static void Test()
    {
        People[] ps0 
= new People[]{
            
new People{ Id=1, Name = "aaa"},
            
new People{ Id=2, Name = "bbb"},
            
new People{ Id=3, Name = "ccc"}
        };
        List
<People> ps1 = new List<People>(ps0);
        Collection
<People> ps2 = new Collection<People>(ps1);
        IEnumerable
<People> ps3 = YeildRet(ps0);
        Dictionary
<People, string> dict = ps0.ToDictionary(p=>p, p=>p.Name);

        People p0 
= new People { Id = 1, Name = "aaa" };

        
//
        bool b0 = p0 == ps0[0];
        
bool b1 = p0.Equals(ps0[0]);
        
//
        bool b2 = ps0.Contains(p0);
        
bool b3 = ps1.Contains(p0);
        
bool b4 = ps2.Contains(p0);
        
bool b5 = ps3.Contains(p0);
        
//
        bool b6 = dict.ContainsKey(p0);
    }

    
private static IEnumerable<People> YeildRet(People[] peoples)
    {
            
yield return peoples[0];
            
yield return peoples[1];
            
yield return peoples[2];
    }

 下面会把People类逐步改进,同样请给出每个版本的b0~b6的值,一个版本一个版本做,做完一个版本才能展开下一个版本。不要作弊奥!

ContractedBlock.gifExpandedBlockStart.gifPeople版本二
    class People
    {
        
public int Id { getset; }
        
public string Name { getset; }

        
public override string ToString()
        {
            
return string.Format("Id: {0}; Name: {1}", Id, Name);
        }

        
public bool Equals(People other)
        {
            
return Id.Equals(other.Id);
        }
    }

再改进,

ContractedBlock.gifExpandedBlockStart.gifPeople版本三
    class People: IEquatable<People>
    {
        
public int Id { getset; }
        
public string Name { getset; }

        
public override string ToString()
        {
            
return string.Format("Id: {0}; Name: {1}", Id, Name);
        }

        
public bool Equals(People other)
        {
            
return Id.Equals(other.Id);
        }
    }

再改进

ContractedBlock.gifExpandedBlockStart.gifPeople版本四
    class People: IEquatable<People>
    {
        
public int Id { getset; }
        
public string Name { getset; }

        
public override string ToString()
        {
            
return string.Format("Id: {0}; Name: {1}", Id, Name);
        }

        
public bool Equals(People other)
        {
            
return Id.Equals(other.Id);
        }
        
        
public override bool Equals(object obj)
        {
            
if (obj is People) return Equals((People)obj);
            
return base.Equals(obj);
        }
    }

再改进:

ContractedBlock.gifExpandedBlockStart.gifPeople版本五
    class People: IEquatable<People>
    {
        
public int Id { getset; }
        
public string Name { getset; }

        
public override string ToString()
        {
            
return string.Format("Id: {0}; Name: {1}", Id, Name);
        }

        
public bool Equals(People other)
        {
            
return Id.Equals(other.Id);
        }
        
        
public override bool Equals(object obj)
        {
            
if (obj is People) return Equals((People)obj);
            
return base.Equals(obj);
        }

        
public static bool operator ==(People left, People right)
        {
            
return left.Equals(right);
        }

        
public static bool operator !=(People left, People right)
        {
            
return !left.Equals(right);
        }
        
        
public override int GetHashCode()
        {
            
return Id.GetHashCode();
        }
    }

 说明:代码仅供测试用,没有进行null值处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值