'Cannot find how to do equality for the object I am dealing with POCS collection of POCS object

Consider a simple POCS object FeeInfo (yes I know an object would normally be called simply Fee, but the usage of it makes the class name FeeInfo make more sense.)

        public FeeInfo()
    {
        Description = String.Empty;
        Status = String.Empty;
    }

    public string Description { get; set; }
    public string Status { get; set; }
    public decimal FeeAmount { get; set; }
    public decimal FeePaid { get; set; }

    public bool Equals(FeeInfo targetFee)
    {
        if (targetFee.Description == this.Description && targetFee.Status == this.Status &&
            targetFee.FeeAmount == this.FeeAmount && targetFee.FeePaid == this.FeePaid)
        {
            return true;
        }

        return false;
    }

I am clearly not using the interfaces for comparing / equality (largely because I don't yet understand them enough to understand if I need/want them. For this object, my equality method works to my needs.

So imagine another object FeesInfo. Where FeesInfo wraps

private readonly List<FeeInfo> _fees = new List<FeeInfo>();

implements :IEnumerable and the like.

I want to be able to compare two FeesInfo objects to ensure that the FeeInfo objects within it are the same. This would be two instances which contain the same number (Count) of elements which is easy. But I want an unordered comparison of the objects whose FeeInfo properties are the same.

What is the best way to achieve this for this not super savy c# programmer? Thanks



Solution 1:[1]

The COMMENT is not an "answer" exactly. But it is THE answer.

First fix your implementation of Equals on your FeeInfo class. You may want to follow this guide: How to define value equality for a class or struct (C# Programming Guide)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2919960