Arrays.cs 2.2 KB

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