Gets the hash code of all items in the array, or zero when null. - CSharp System

CSharp examples for System:Array Null Element

Description

Gets the hash code of all items in the array, or zero when null.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;//www.  j  av  a  2  s  . c  o  m

public class Main{
        /// <summary>
        /// Gets the hash code of all items in the array, or zero when null.
        /// </summary>
        public static int GetHashCodeOfItemsIfExists(IList array)
        {
            if (ReferenceEquals(array, null))
                return 0;
            return array.GetHashCodeOfItems();
        }
        /// <summary>
        /// Gets the hash code of all items in the array.
        /// </summary>
        public static int GetHashCodeOfItems(this IList array)
        {
            return array.Cast<object>().Aggregate(0, (current, item) => current ^ (!ReferenceEquals(item, null) ? item.GetHashCode() : 0));
        }
}

Related Tutorials