ObjectIntPair.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. namespace Google.Protobuf
  3. {
  4. /// <summary>
  5. /// Struct used to hold the keys for the fieldByNumber table in DescriptorPool and the keys for the
  6. /// extensionByNumber table in ExtensionRegistry.
  7. /// </summary>
  8. internal struct ObjectIntPair<T> : IEquatable<ObjectIntPair<T>> where T : class
  9. {
  10. private readonly int number;
  11. private readonly T obj;
  12. internal ObjectIntPair(T obj, int number)
  13. {
  14. this.number = number;
  15. this.obj = obj;
  16. }
  17. public bool Equals(ObjectIntPair<T> other)
  18. {
  19. return obj == other.obj
  20. && number == other.number;
  21. }
  22. public override bool Equals(object obj)
  23. {
  24. if (obj is ObjectIntPair<T>)
  25. {
  26. return Equals((ObjectIntPair<T>)obj);
  27. }
  28. return false;
  29. }
  30. public override int GetHashCode()
  31. {
  32. return obj.GetHashCode() * ((1 << 16) - 1) + number;
  33. }
  34. }
  35. }