C# Data Structures Fundamental

In the .NET Framework we have data structures like dictionary, array, stack, hashtable, queue, linkedlist etc.

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)
{
	Console.WriteLine(s.Firstname);
}

Here is the output of above code.

/*
result will be like:
Name 4
Name 5
Name 6
Name 1
Name 3
Name 2
*/
 
C# Delegates Example
Online Tutorials
C# .net Interview Questions Answers
Multicast Delegate Example
.Net C# Examples | Join .Net C# Course