0

I have a dictionary which stores method call results. The Dictionary key is the method call signature i.e. an object with the following members.

string _name, 
Type[] _argTypes, 
string[] _argNames, 
object[] _argValues.

I must override the GetHashCode method for the TryGetValue to compare correctly. What is the most efficient way to compare the method signatures? i.e. the 4 parameters above.

e.g. this does not seem efficient to me.

 public override int GetHashCode()
        {
            if(_name!=null)
                _hashCode = _name.GetHashCode();

            if (_argTypes != null)
            {
                foreach (object value in _argTypes)
                    _hashCode ^= value.GetHashCode();
            }
            if (_argNames != null)
            {
                foreach (object value in _argNames)
                    _hashCode ^= value.GetHashCode();
            }
            if (_argValues != null)
            {
                foreach (object value in _argValues)
                    _hashCode ^= value.GetHashCode();
            }
        }
Chris McCall
  • 10,317
  • 8
  • 49
  • 80
learnerplates
  • 4,257
  • 5
  • 33
  • 42
  • 1
    At the very least implement GetHashCode in a recommended way: http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode - And have you thought about just calculating it once and then caching it? – Lasse V. Karlsen Apr 30 '14 at 14:48
  • this is called "memoization", knowing that may improve search results – Chris McCall Apr 30 '14 at 14:54
  • Don't you need to override `Equals` as well? `GetHashCode()` is just used for uniform distribution. Once you have a hash, you need to have an `Equals` implementation that compares your method signature for equality. – Sourav 'Abhi' Mitra Apr 30 '14 at 14:57

0 Answers0