PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 1ms

/Utilities/Collections/ListExtensions.cs

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