| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ReferenceEqualityComparer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Collections; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Primitives.Comparers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides a default instance of a reference equality comparer for objects. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class ReferenceEqualityComparer |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets a singleton instance of <see cref="ReferenceEqualityComparer"/>. |
| | | 20 | | /// </summary> |
| | 3 | 21 | | public static ReferenceEqualityComparer<object> Instance { get; } = new(); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// An equality comparer that compares objects by reference rather than by value. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <typeparam name="T">The type of objects to compare.</typeparam> |
| | | 28 | | public class ReferenceEqualityComparer<T> : IEqualityComparer, IEqualityComparer<T> |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="ReferenceEqualityComparer{T}"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | internal ReferenceEqualityComparer() { } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Returns a hash code for the specified object based on its reference. |
| | | 37 | | /// </summary> |
| | | 38 | | public int GetHashCode(object obj) => RuntimeHelpers.GetHashCode(obj); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Determines whether the specified objects are the same instance (reference equality). |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="x">The first object to compare.</param> |
| | | 44 | | /// <param name="y">The second object to compare.</param> |
| | | 45 | | /// <returns><c>true</c> if the specified objects are the same instance; otherwise, <c>false</c>.</returns> |
| | | 46 | | bool IEqualityComparer.Equals(object? x, object? y) => ReferenceEquals(x, y); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Determines whether the specified objects of type <typeparamref name="T"/> are the same instance (reference equal |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="x">The first object to compare.</param> |
| | | 52 | | /// <param name="y">The second object to compare.</param> |
| | | 53 | | /// <returns><c>true</c> if the specified objects are the same instance; otherwise, <c>false</c>.</returns> |
| | | 54 | | bool IEqualityComparer<T>.Equals(T? x, T? y) => ReferenceEquals(x, y); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Returns a hash code for the specified object of type <typeparamref name="T"/> based on its reference. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="obj">The object for which to get the hash code.</param> |
| | | 60 | | /// <returns>A hash code for the specified object.</returns> |
| | | 61 | | int IEqualityComparer<T>.GetHashCode(T? obj) => RuntimeHelpers.GetHashCode(obj); |
| | | 62 | | } |
| | | 63 | | |