PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/Collections/ListExtensions.cs

#
C# | 60 lines | 42 code | 3 blank | 15 comment | 4 complexity | 4f8028351517a4eafeb8cf7f68b08f4b MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using NUnit.Framework;
  3. namespace Delta.Utilities.Collections
  4. {
  5. /// <summary>
  6. /// Class extensions
  7. /// </summary>
  8. public static class ListExtensions
  9. {
  10. #region Clone (Static)
  11. /// <summary>
  12. /// Clone given list via array copy and return a new list that has each
  13. /// item copied into (deep copy).
  14. /// </summary>
  15. /// <param name="anyList">Any list</param>
  16. /// <returns>Cloned list</returns>
  17. public static List<T> Clone<T>(this List<T> anyList)
  18. {
  19. if (anyList == null ||
  20. anyList.Count == 0)
  21. {
  22. return new List<T>();
  23. }
  24. else
  25. {
  26. T[] dataCopy = new T[anyList.Count];
  27. anyList.CopyTo(dataCopy);
  28. return new List<T>(dataCopy);
  29. }
  30. }
  31. #endregion
  32. /// <summary>
  33. /// Tests
  34. /// </summary>
  35. internal class ListExtensionsTests
  36. {
  37. #region Clone
  38. /// <summary>
  39. /// Clone
  40. /// </summary>
  41. [Test]
  42. public void Clone()
  43. {
  44. List<int> data = new List<int>(new[]
  45. {
  46. 0, 1, 2,
  47. });
  48. List<int> clonedData = data.Clone();
  49. for (int index = 0; index < data.Count; index++)
  50. {
  51. Assert.Equal(clonedData[index], data[index]);
  52. }
  53. }
  54. #endregion
  55. }
  56. }
  57. }