避免在列表中添加自定义对象之前在列表中添加重复的对象?

时间:2018-09-30 02:42:59

标签: c#

我在互联网上写了几篇文章,但对我都很有价值,我不明白如何避免将重复的对象添加到列表中,我尝试过类似的事情。

我实际上已经创建了一个覆盖GetHashCode和Equal方法的类。

现在我要形成一个非重复对象列表的集合。

public class FlightInfo
    {
        public string Origin { get; set; }

        public string DepartureTime { get; set; }

        public string Destination { get; set; }

        public string DestinationTime { get; set; }

        public string Price { get; set; }

        public override bool Equals(object obj)
        {
            var other = obj as FlightInfo;

            if (other == null)
                return false;

            if (Origin != other.Origin || DepartureTime != other.DepartureTime || Destination != other.Destination
                || DestinationTime != other.DestinationTime || Price != other.Price)
                return false;

            return true;
        }

        public override int GetHashCode()
        {
            int hashOrigin = Origin.GetHashCode();
            int hashDestination = Destination.GetHashCode();

            int hashDepartureTime = DepartureTime.GetHashCode();

            int hashDestinationTime = DestinationTime.GetHashCode();

            int hashPrice = Price.GetHashCode();

            return hashOrigin ^ hashDestination ^ hashDepartureTime ^ hashDestinationTime ^ hashPrice;
        }
    }

我也尝试了Eric的一篇文章

https://blogs.msdn.microsoft.com/ericlippert/2011/02/28/guidelines-and-rules-for-gethashcode/

但是本文有

private List<T>[] buckets = new List<T>[100];

私人List<T>() buckets = new List<T>()

的入侵

但是我想返回一个没有固定大小的列表。

1 个答案:

答案 0 :(得分:0)

由于您已经实现了EqualsGetHashCode方法,因此您可以拥有自己的FlightInfo自定义列表,这些列表将使用这些方法:

public class FlightInfoList : IList<FlightInfo>
{
    private readonly List<FlightInfo> _flightInfos = new List<FlightInfo>();

    public IEnumerator<FlightInfo> GetEnumerator()
    {
        return _flightInfos.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Add(FlightInfo item)
    {
        if (_flightInfos.Any(flightInfo => flightInfo.Equals(item)))
        {
            throw new Exception("Cannot add duplicated values!");
        }

        _flightInfos.Add(item);
    }

    public void Clear()
    {
        _flightInfos.Clear();
    }

    public bool Contains(FlightInfo item)
    {
        return _flightInfos.Contains(item);
    }

    public void CopyTo(FlightInfo[] array, int arrayIndex)
    {
        _flightInfos.CopyTo(array, arrayIndex);
    }

    public bool Remove(FlightInfo item)
    {
        return _flightInfos.Remove(item);
    }

    public int Count => _flightInfos.Count;

    public bool IsReadOnly => false;

    public int IndexOf(FlightInfo item)
    {
        return _flightInfos.IndexOf(item);
    }

    public void Insert(int index, FlightInfo item)
    {
        _flightInfos.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        _flightInfos.RemoveAt(index);
    }

    public FlightInfo this[int index]
    {
        get => _flightInfos[index];
        set => _flightInfos[index] = value;
    }
}

请注意,在Add方法中,我正在检查是否存在重复项。解决此问题的另一种方法是使用字典。

相关问题