CanonicalEquatableDerivedClass.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. namespace MongoDB.Shared
  17. {
  18. internal class CanonicalEquatableDerivedClass : CanonicalEquatableClass, IEquatable<CanonicalEquatableDerivedClass>
  19. {
  20. // private fields
  21. private int _z;
  22. // constructors
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="CanonicalEquatableDerivedClass"/> class.
  25. /// </summary>
  26. /// <param name="x">The x.</param>
  27. /// <param name="y">The y.</param>
  28. /// <param name="z">The z.</param>
  29. public CanonicalEquatableDerivedClass(int x, int y, int z)
  30. : base(x, y)
  31. {
  32. _z = z;
  33. }
  34. // base class defines == and !=
  35. // public methods
  36. /// <summary>
  37. /// Determines whether the specified <see cref="CanonicalEquatableDerivedClass" /> is equal to this instance.
  38. /// </summary>
  39. /// <param name="obj">The <see cref="CanonicalEquatableDerivedClass" /> to compare with this instance.</param>
  40. /// <returns>
  41. /// <c>true</c> if the specified <see cref="CanonicalEquatableDerivedClass" /> is equal to this instance; otherwise, <c>false</c>.
  42. /// </returns>
  43. public bool Equals(CanonicalEquatableDerivedClass obj)
  44. {
  45. return Equals((object)obj);
  46. }
  47. /// <summary>
  48. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  49. /// </summary>
  50. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  51. /// <returns>
  52. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  53. /// </returns>
  54. public override bool Equals(object obj)
  55. {
  56. // base class checks for obj == null and correct type
  57. if (!base.Equals(obj)) { return false; }
  58. var rhs = (CanonicalEquatableDerivedClass)obj;
  59. return // be sure z implements ==, otherwise use Equals
  60. _z == rhs._z;
  61. }
  62. /// <summary>
  63. /// Returns a hash code for this instance.
  64. /// </summary>
  65. /// <returns>
  66. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  67. /// </returns>
  68. public override int GetHashCode()
  69. {
  70. // use hash code of base class as seed to Hasher
  71. return new Hasher(base.GetHashCode())
  72. .Hash(_z)
  73. .GetHashCode();
  74. }
  75. }
  76. }