Arrays.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. namespace Wayne.Lib
  2. {
  3. /// <summary>
  4. /// Array extension methods.
  5. /// </summary>
  6. public static class Arrays
  7. {
  8. #region Methods
  9. /// <summary>
  10. /// Compares two arrays to see if they are equal. (If both are null it also returns true.)
  11. /// </summary>
  12. /// <typeparam name="TDataType">The datatype of the array.</typeparam>
  13. /// <param name="array1">One of the arrays to compare.</param>
  14. /// <param name="array2">The other array to compare.</param>
  15. /// <returns></returns>
  16. public static bool Equal<TDataType>(TDataType[] array1, TDataType[] array2)
  17. {
  18. if ((array1 == null) && (array2 == null))
  19. return true;
  20. if ((array1 != null) && (array2 != null) && (array1.Length == array2.Length))
  21. {
  22. for (int i = 0; i < array1.Length; i++)
  23. if (!array1[i].Equals(array2[i]))
  24. return false;
  25. return true;
  26. }
  27. return false;
  28. }
  29. /// <summary>
  30. /// Fill an array with a certain value.
  31. /// </summary>
  32. /// <typeparam name="TDataType">The datatype of the array.</typeparam>
  33. /// <param name="array">The array to fill.</param>
  34. /// <param name="value">The value to fill with.</param>
  35. public static void Fill<TDataType>(TDataType[] array, TDataType value)
  36. {
  37. if ((array != null) && (array.Length > 0))
  38. {
  39. for (int i = 0; i < array.Length; i++)
  40. array[i] = value;
  41. }
  42. }
  43. /// <summary>
  44. /// Get the hashcode of an array for comparision.
  45. /// </summary>
  46. /// <typeparam name="TDataType">The datatype of the array.</typeparam>
  47. /// <param name="array">The array to get the hashcode of.</param>
  48. public static int GetHashCode<TDataType>(TDataType[] array)
  49. {
  50. int hashCode = 0;
  51. if (array != null)
  52. {
  53. for (int i = 0; i < array.Length; i++)
  54. hashCode ^= array[i].GetHashCode();
  55. }
  56. return hashCode;
  57. }
  58. #endregion
  59. }
  60. }