Join Class

C# Data Structures Fundamental

In the .NET Framework we have data structures like dictionary, array, stack, hashtable, queue, linkedlist etc. Each structure allows us to play with collection of data with different principles, let’s learn each of them one by one.

  • C# Arrays

    Various ways we can define array in c# program.

    int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  • C# Stack

    C# Stack data structure stores elements in "Last in First Out" order, known as "LIFO style". Stack class comes under System.Collections.Generic namespace.

    using System.Collections;
    Stack myStack = new Stack();

  • C# Queue

    There are two type of Queue class in c#, Queue can be generic and non-generic.

    using System;
    using System.Collections.Generic;
    Queue<string> numbers = new Queue<string>();

  • C# Hashtable

    Hashtable data structure in C# allow us to store data using key value pairs.

    Hashtable htObj = new Hashtable();
    htObj.Add(1, "This is string");

  • C# Generic Example

    Generic data structure was not available in earlier version of c# , Generic was introduced in .net 2.0, this helps dealing with different type of strongly type data types.

    class GenericExample
    public static List<T> MyGenericMethod<T>(T value, int maxCount)
    { List<T> list = new List<T>();
    for (int i = 0; i < maxCount; i++)
            return list;

  • LinkedList

    LinkedList comes under System.Collections.Generic namespace. using LinkedList, we can remove node and reinsert in the same list, which results in no additional objects allocated on the heap.

    string[] words ={ "the", "fox", "jumps", "over", "the", "dog" };
    LinkedList<string> sentence = new LinkedList<string>(words);

  • C# Dictionary

    Dictionary is a collection object just like list object; using dictionary object, we can store values with key value pair.

    IDictionary<int, string> _dictObj = new Dictionary<int, string>();

There are more data structure can be found in NuGet packages, some are available at Github library.

In above list I explained how to use each different object in C# programming, now here let’s understand some difference among them.

Difference between Stack and Queue in C#
1. A Stack is an ordered list of elements where all insertions and deletions are made at the same end.
1. Queue is exactly the opposite of a Stack, means Queue object is used to insert data at one end, and to remove data from other end.
2. A Stack follows the LIFO (Last In First Out) principle.
2. Queue data structure follows the FIFO (First In First Out) principle
3. To add remove item from Stack item we use Push and Pop method.
3. To add remove item from Queue object we use enqueue and Pop dequeue methods.
difference between Hashtable and Dictionary in c#
1. A Hashtable is non-generic collection, Hashtable comes under System.Collections namespace.
1. A Dictionary is generic collection, Dictionary comes under System.Collections.Generic namespace.
2. In Hashtable, we can store key-value pairs of any data type, same or different.
2. In Dictionary, we can store key-value pairs of same data type only.
3. Hashtable doesn't maintain the order of stored values.
3. Dictionary maintain the order of stored values.
LinkedList data structure

There is another not so popular datastructure in c# is LinkedList LinkedList is the data structure which can contain a group of nodes in a sequence and each node contains two parts.

LinkedList is comparatively faster to access item from the list, Insertion and deletion is easy, we also can use other data structures such as Stack and Queue to be implemented easily using LinkedList.

static LinkedList<Student> studentList = new LinkedList<Student>();

LinkedListNode<Student> stuNode;

stuNode= studentList.AddFirst(new Student() { Firstname = "Name 1" });
studentList.AddLast(new Student() { Firstname = "Name 2" });
studentList.AddAfter(stuNode, new Student() { Firstname = "Name 3" });
studentList.AddBefore(stuNode,new Student() { Firstname = "Name 4" });
studentList.AddBefore(stuNode, new Student() { Firstname = "Name 5" });
studentList.AddBefore(stuNode, new Student() { Firstname = "Name 6" });

foreach (Student s in studentList)

Here is the output of above code.

result will be like:
Name 4
Name 5
Name 6
Name 1
Name 3
Name 2
C# data structures
.Net C# Programming Tutorials
Online Tutorials
C# .net Interview Questions Answers
.Net C# Examples | Join .Net C# Course