Get Hash Code for IEnumerable - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Get Hash Code for IEnumerable

Demo Code

// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

public class Main{
        // We could probably improve the hashing here
        public static int ListHashCode<T>(this IEnumerable<T> list)
        {/*from w w w .  j a v  a 2s .  com*/
            var cmp = EqualityComparer<T>.Default;
            int h = 6551;
            foreach (T t in list)
            {
                h ^= (h << 5) ^ cmp.GetHashCode(t);
            }
            return h;
        }
}

Related Tutorials