Arrays and Generic Lists in C# (Part1 )

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

 

Change in focus : domscode.com

Not a very interesting post, more a clarrification on the scope of the blog. I originally created the blog as a resource for .NET and iPhone development, but with work and family commitments its hard to extend my iPhone skills at the moment and rather than try and maintain a 50% split – I am now going to cover my major interests of web, mobile and games. For the record,  I am not dropping iPhone development – I just don’t have as much  time as I would like to devote  to XCode/Objective C. Also, Whilst I have only really dappled in games development – its a major interest for me and the vast number of my tutorials are based on games, I think its fair to extend this focus. So what will change – in some ways not a lot, although I expect to extend the focus onto XNA development. Now XNA (Xbox, Windows Phone and Windows game development) is all .NET based anyway so thats not a hug change from present.

Dom

Criticism against the N-Layered App

I blogged a couple of weeks ago about an excellent tutorial on enterprise scale development. In the last couple of days I have come across some significant criticism of this resource and I think its important to highlight this critism (right or wrong) particularly as the two authors are very significent members of the .NET community (Rob Connery and

http://wekeroad.com/post/7102729511/a-simple-example-thats-incredibly-complex

and

http://ayende.com/blog/tags/reviews (scroll down to the first of 10 blogs on the subject of Review: Microsoft N Layer App Sample)

I will make a response in the next day or so

Dom