csharp/42skillz/Diverse/Diverse/ArgumentHasher.cs

ArgumentHasher.cs
using System.Collections;

namespace Diverse
{
    /// 
    /// Hash arguments (so that we can Memoize).
    /// 
    public clast ArgumentHasher
    {
        /// 
        /// Hash provided arguments.
        /// 
        /// the 
        /// 
        public static int HashArguments(params object[] arguments)
        {
            var hash = 17;
            foreach (var argument in arguments)
            {
                if (argument is IEnumerable enumerable)
                {
                    var parameterHashCode = GetByValueHashCode(enumerable);
                    hash = hash * 23 + parameterHashCode;
                }
                else
                {
                    var parameterHashCode = argument?.GetHashCode() ?? 17;
                    hash = hash * 23 + parameterHashCode;
                }
            }

            return hash;
        }

        /// 
        /// Handle the case of collections (where we want to compare their content and not the reference of the collection).
        /// 
        /// 
        /// 
        private static int GetByValueHashCode(IEnumerable collection)
        {
            var hash = 17;

            foreach (var element in collection)
            {
                var parameterHashCode = element?.GetHashCode() ?? 17;
                hash = hash * 23 + parameterHashCode;
            }

            return hash;
        }
    }
}