I have had a request to discuss C# arrays but in general I don’t use arrays much anymore. I use generic Lists all the time mainly because the list is dynamic and can be resized easily. Arrays in comparrison are a static amount of memory and need to resized to handle additional items. The following is a simple console app (all it does is sort an group of ints, add another int, sort again and then print the list) that demonstrates what I am talking about – no linq yet I’ll add that to part 2 …
using System; using System.Collections.Generic; namespace ArraysAndLists { class Program { static void Main(string[] args) { //Why I don't really use arrays any more - //we will sort an array add another item and sort again int[] numberArray = { 4,1,3, 1, 3, 4, 5 }; Array.Sort(numberArray); int[] numberArray2 = new int[numberArray.Length+1]; numberArray.CopyTo(numberArray2,0); numberArray2[numberArray2.Length - 1] = 2; Array.Sort(numberArray2); Console.WriteLine("Array Sorted"); for (int i = 0; i < numberArray2.Length;i++ ) { Console.WriteLine(numberArray2[i]); } //Why I love generic lists //we will sort an array add another item and sort again List<int> numberList = new List<int> { 4, 1, 3, 1, 3, 4, 5 }; numberList.Sort(); numberList.Add(2); numberList.Sort(); Console.WriteLine("Generic List Sorted"); numberList.ForEach(i => Console.WriteLine(i)); Console.ReadLine(); } } }
I did find a great article on datastructures in C# btw, and in fact its part of a game development series so if you are interested in a more detailed analysis of .NET collection classes (Arrays,Collections,Lists etc…) have a look
http://create.msdn.com/en-US/education/catalog/article/data_structures
Cheers
Dom