Full implementation of the operator == for a structure.
2011-Жов-10, Понеділок 15:17![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
In my example ExternalRefId is unique. That's why other fields (i.e. ExternalRefValue :) are ignored.
- public struct ReconcileInfo
- {
- public string ExternalRefValue;
- public int ExternalRefId;
- /// <summary>
- /// Initializes a new instance of the <see cref="ReconcileInfo"/> struct.
- /// </summary>
- /// <param name="externalRefId">The external ref id.</param>
- /// <param name="externalRefValue">The external ref value.</param>
- public ReconcileInfo(int externalRefId, string externalRefValue)
- {
- ExternalRefId = externalRefId;
- ExternalRefValue = externalRefValue;
- }
- /// <summary>
- /// Implements the operator ==.
- /// </summary>
- /// <param name="left">The left.</param>
- /// <param name="right">The right.</param>
- /// <returns>
- /// The result of the operator.
- /// </returns>
- public static bool operator ==(ReconcileInfo left, ReconcileInfo right)
- {
- return left.ExternalRefId == right.ExternalRefId;
- }
- /// <summary>
- /// Implements the operator !=.
- /// </summary>
- /// <param name="left">The left.</param>
- /// <param name="right">The right.</param>
- /// <returns>
- /// The result of the operator.
- /// </returns>
- public static bool operator !=(ReconcileInfo left, ReconcileInfo right)
- {
- return !(left == right);
- }
- /// <summary>
- /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
- /// </summary>
- /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
- /// <returns>
- /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
- /// </returns>
- public override bool Equals(object obj)
- {
- if (!(obj is ReconcileInfo))
- {
- return false;
- }
- ReconcileInfo objAsStruct = (ReconcileInfo)obj;
- return objAsStruct.ExternalRefId == this.ExternalRefId;
- }
- /// <summary>
- /// Returns a hash code for this instance.
- /// </summary>
- /// <returns>
- /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
- /// </returns>
- public override int GetHashCode()
- {
- return this.ExternalRefId.GetHashCode();
- }
- }