Use byte[] as key in dictionary

前端 未结 7 1592
南旧
南旧 2020-12-28 12:11

I need to use a byte[] as a key in a Dictionary. Since byte[] doesn\'t override the default GetHashCode method, two sepa

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 12:59

    using System;
    using System.Collections;
    using System.Collections.Generic;
    
    [Serializable]
    class StructuralEqualityComparer : IEqualityComparer, IEqualityComparer
    {
        public new bool Equals(object x, object y)
        {
            var s = x as IStructuralEquatable;
            return s == null ? object.Equals(x, y) : s.Equals(y, this);
        }
    
        public int GetHashCode(object obj)
        {
            var s = obj as IStructuralEquatable;
            return s == null ? EqualityComparer.Default.GetHashCode(obj) : s.GetHashCode(this);
        }
    }
    
        

    提交回复
    热议问题