List Hash Code - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:List

Description

List Hash Code

Demo Code

// Copyright (c) Microsoft. All rights reserved.
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   www . j  a  va 2 s. co m*/
            var cmp = EqualityComparer<T>.Default;
            int h = 6551;
            foreach (T t in list)
            {
                h ^= (h << 5) ^ cmp.GetHashCode(t);
            }
            return h;
        }
}

Related Tutorials