/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
- using System.Collections.Generic;
- using NUnit.Framework;
-
- namespace Delta.Utilities.Collections
- {
- /// <summary>
- /// Class extensions
- /// </summary>
- public static class ListExtensions
- {
- #region Clone (Static)
- /// <summary>
- /// Clone given list via array copy and return a new list that has each
- /// item copied into (deep copy).
- /// </summary>
- /// <param name="anyList">Any list</param>
- /// <returns>Cloned list</returns>
- public static List<T> Clone<T>(this List<T> anyList)
- {
- if (anyList == null ||
- anyList.Count == 0)
- {
- return new List<T>();
- }
- else
- {
- T[] dataCopy = new T[anyList.Count];
- anyList.CopyTo(dataCopy);
- return new List<T>(dataCopy);
- }
- }
- #endregion
-
- /// <summary>
- /// Tests
- /// </summary>
- internal class ListExtensionsTests
- {
- #region Clone
- /// <summary>
- /// Clone
- /// </summary>
- [Test]
- public void Clone()
- {
- List<int> data = new List<int>(new[]
- {
- 0, 1, 2,
- });
- List<int> clonedData = data.Clone();
-
- for (int index = 0; index < data.Count; index++)
- {
- Assert.Equal(clonedData[index], data[index]);
- }
- }
- #endregion
- }
- }
- }