1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- namespace Wayne.Lib
- {
- /// <summary>
- /// Array extension methods.
- /// </summary>
- public static class Arrays
- {
- #region Methods
- /// <summary>
- /// Compares two arrays to see if they are equal. (If both are null it also returns true.)
- /// </summary>
- /// <typeparam name="TDataType">The datatype of the array.</typeparam>
- /// <param name="array1">One of the arrays to compare.</param>
- /// <param name="array2">The other array to compare.</param>
- /// <returns></returns>
- public static bool Equal<TDataType>(TDataType[] array1, TDataType[] array2)
- {
- if ((array1 == null) && (array2 == null))
- return true;
- if ((array1 != null) && (array2 != null) && (array1.Length == array2.Length))
- {
- for (int i = 0; i < array1.Length; i++)
- if (!array1[i].Equals(array2[i]))
- return false;
- return true;
- }
- return false;
- }
- /// <summary>
- /// Fill an array with a certain value.
- /// </summary>
- /// <typeparam name="TDataType">The datatype of the array.</typeparam>
- /// <param name="array">The array to fill.</param>
- /// <param name="value">The value to fill with.</param>
- public static void Fill<TDataType>(TDataType[] array, TDataType value)
- {
- if ((array != null) && (array.Length > 0))
- {
- for (int i = 0; i < array.Length; i++)
- array[i] = value;
- }
- }
- /// <summary>
- /// Get the hashcode of an array for comparision.
- /// </summary>
- /// <typeparam name="TDataType">The datatype of the array.</typeparam>
- /// <param name="array">The array to get the hashcode of.</param>
- public static int GetHashCode<TDataType>(TDataType[] array)
- {
- int hashCode = 0;
- if (array != null)
- {
- for (int i = 0; i < array.Length; i++)
- hashCode ^= array[i].GetHashCode();
- }
- return hashCode;
- }
- #endregion
- }
- }
|