Common C# Data Structures [Quick Tutorial + Code Challenges]

When building applications as a developer, you should always be cautious about what data structures you use to make sure that it matches the data type and purpose. This article covers the common data structures for C#, when these should be used, and the advantages one has over the other.

Arrays

Arrays are the simplest form of data structures. Arrays are collections of elements, and while the size can dynamically resize, this is not for free. If the size of the arrays always has to be updated, this can cause huge performance hits within your application. This is because new parts of the memory have to be allocated, data has to be copied over to the new memory space, and the old memory section needs to be freed up.

Here is an example of how a simple array (myNumbers) can be created and used. The array has a length property, which stores how many elements are in the array. The items in the array can be accessed using the index[], but this may raise ArgumentOutOfRangeException if you pass in a number that exceeds the number of items in the collection.

public static void UsingArrays() {
	int[] myNumbers = { 1, 2, 3, 4 };
	Console.WriteLine("Length of the myNumbers array is: " + myNumbers.Length);
	var secondNumber = myNumbers[1]; //arrays are 0 index based

	//arrayList
	ArrayList myNames = new ArrayList();
	myNames.Add("John Doe");
	myNames.AddRange(new string[] { "Jane Doe", "Danny Doe" });

	Console.Write("Here are some names:");
	LogArray(myNames);

	myNames.Insert(0, "Frank Doe");
	myNames.Insert(0, "Emma Doe");
	myNames.Insert(0, "Bob Doe");
	Console.WriteLine("\nLength of the myNames array is: " + myNames.Count);

	Console.Write("Here are some names again:");
	LogArray(myNames);

	var grandpasIdx = myNames.BinarySearch("Bob Doe");
	Console.Write("\nFound Grandpa's name on {0} position in the myNames array", grandpasIdx);
}

Next, you can see how ArrayList is used.  The ArrayList is a collection that implements the IList interface, but it stores the items in an array and it automatically resizes if items are added to the collection, like when the Insert() or Add() methods are used. 

Code Challenge: Try to implement you own ArrayList class using a classic array, like myNumbers in the code above, then implement the IList interface and the dynamic resize of the collection when items are added.

Lists

There are generics in C# (in C++ you call them templates) that are really helpful when working with Lists. C# has the List<T>, LinkedList<T> implementations of Lists (besides ArrayList). 

The standard List<T> uses an array behind the scenes (implementation) to hold the values that are added to it. This data structure can be useful if you need random access of data, because the array's items can be instantly accessed using the indexes. The drawback of List<T> is that when you need to add many items to the middle of the list, the second half of the array has to be shifted. This could take longer, because a new memory partition has to be allocated and the data has to copied. 

The LinkedList<T> is a standard linked list implementation that offers a better choice if you need to access the data sequentially. That's because in this case, you have to visit all the nodes of the list, which can be very fast. 

Greg Bogdan
Greg Bogdan Hire Me

Software Engineer, Blogger, Tech Enthusiast

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript. I like technical writing and have good experience in creating tutorials and how to technical articles. I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...

Hire Me

Next Article

PHP Symfony [Guide]