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.
Various ways we can define array in c# program.
int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
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();
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>(); numbers.Enqueue("India");
Hashtable data structure in C# allow us to store data using key value pairs.
Hashtable htObj = new Hashtable(); htObj.Add(1, "This is string");
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++) { list.Add(value); } return list; } }
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);
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.
System.Collections
namespace.
System.Collections.Generic
namespace.
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 */