/// <summary>
        ///     Ensures that the specified parameter is not equal to the other specified value, or otherwise throws an <see cref="ArgumentException" />.
        ///     The specified <paramref name="equalityComparer" /> is used for comparison (both GetHashCode and Equals).
        /// </summary>
        /// <typeparam name="T">The type of the parameter to be checked.</typeparam>
        /// <param name="parameter">The parameter to be checked.</param>
        /// <param name="other">The value that <paramref name="parameter" /> is checked against.</param>
        /// <param name="equalityComparer">The equality comparer that is used for comparing (optional). If null is specified, then <see cref="EqualityComparer{T}.Default"/> is used.</param>
        /// <param name="parameterName">The name of the parameter (optional).</param>
        /// <param name="message">
        ///     The message that should be injected into the <see cref="ArgumentException" /> (optional).
        /// </param>
        /// <param name="exception">
        ///     The exception that will be thrown when the comparison fails (optional).
        ///     Please note that <paramref name="message" /> and <paramref name="parameterName" /> are both ignored when you specify exception.
        /// </param>
        /// <exception cref="ArgumentException">
        ///     Thrown when the specified parameter is different from the other value and no <paramref name="exception" /> is specified.
        /// </exception>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="equalityComparer" /> is null.</exception>
        public static T MustNotBe <T>(this T parameter, T other, IEqualityComparer <T> equalityComparer = null, string parameterName = null, string message = null, Func <Exception> exception = null)
        {
            equalityComparer = equalityComparer ?? EqualityComparer <T> .Default;

            if (equalityComparer.EqualsWithHashCode(parameter, other) == false)
            {
                return(parameter);
            }

            throw exception?.Invoke() ?? new ArgumentException(message ?? $"{parameterName ?? "The value"} must not be {other}, but you specified this very value.", parameterName);
        }
        public static bool SetIfDifferent <T>(this IRaisePropertyChanged target, ref T field, T value, IEqualityComparer <T> comparer = null, [CallerMemberName] string memberName = null)
        {
            target.MustNotBeNull(nameof(target));
            comparer = comparer ?? EqualityComparer <T> .Default;

            if (comparer.EqualsWithHashCode(field, value))
            {
                return(false);
            }

            field = value;
            target.OnPropertyChanged(memberName);
            return(true);
        }
Example #3
0
        /// <summary>
        ///     Gets the index of the specified item, or -1 if the it cannot be found.
        /// </summary>
        /// <typeparam name="T">The item type of the collection.</typeparam>
        /// <param name="collection">The collection that might contain the item.</param>
        /// <param name="item">The item whose index is searched for.</param>
        /// <param name="equalityComparer">
        ///     The equality comparer used to compare the items (optional). By default, <see cref="EqualityComparer{T}.Default" /> is used.
        /// </param>
        /// <returns>The index of the specified item, or -1 when the item could not be found.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="collection" /> is null.</exception>
        public static int IndexOf <T>(this IReadOnlyList <T> collection, T item, IEqualityComparer <T> equalityComparer = null)
        {
            collection.MustNotBeNull(nameof(collection));
            equalityComparer = equalityComparer ?? EqualityComparer <T> .Default;

            for (var i = 0; i < collection.Count; i++)
            {
                if (equalityComparer.EqualsWithHashCode(item, collection[i]))
                {
                    return(i);
                }
            }

            return(-1);
        }