Data Structures in C#

This is not an exhaustive list, just listing the most commonly used ones

Why it matters ?

Data structures gives ways to collect and organise data so that various operations can be performed on those data

The data structure has an impact on the performance of an algorithm

What is an algorithm ?

It is a set of predefined instructions to be performed in a given order to accomplish a task

The performance of an algorithm can be measured by :

Time complexity

Space complexity

The Big O notation

Asymptotic Notation (!!simplified here!!)

Algorithm complexity is rough estimation of the number of steps performed by given computation depending on the size of the input data measured through asymptotic notation
Examples:
Linear complexity O(n) – all elements are processed once (or constant number of times)
Quadratic complexity O(n2) – each of the elements is processed n times

Complexity Notation Description
constant O(1) Constant number of operations, whatever the size of the collection
logarithmic O(log n) Logarithmic increase of operations
linear O(n) Number of operations proportional to the input data size
quadratic
cubic
O(n²)
O(n³)
Number of operations proportional to the square (cube )of the size of the input data
exponential
O(2n),
O(kn),
O(n!)
Exponential number of operations, fast growing

Array : T[]

https://msdn.microsoft.com/en-us/library/9b9dty7d.aspx

Stores multiple variables of the same type

It can be single or multi dimensional

The size of the array is fixed at instantiation time

It is zero based indexed and allow random access (by index)

Use when fixed number of elements should be processed by index

ArrayList

https://msdn.microsoft.com/en-us/library/system.collections.arraylist%28v=vs.110%29.aspx

Wraps an Array and can store object of various types

It accepts null as a valid value and allows duplicate elements

List<T>

https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx

It is a collection of object of homogeneous type

List<T> class performs better in most cases and is type safe compared to ArrayList

Use when elements should be added and processed by index

LinkedList<T>

https://msdn.microsoft.com/en-us/library/he2s3bh7%28v=vs.110%29.aspx

Doubly linked node-pointer based collection of data

Relies on the LinkedListNode<T> class

Each node point to the previous and next node

Allows duplicate and null values

No random access possible, accessing a member requires traversing the list

Use when elements should be added at both sides of the list

Dictionary<TKey, TValue>

https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx

Represents a collection of keys and values

Implemented as a hash table, retrieving a value by key fast, close to O(1)

Keys must be not null & unique, values can be null

Iteration returns a KeyValuePair<TKey, TValue> object

Use when key-value pairs should be added fast and searched fast by key

Hashtable

https://msdn.microsoft.com/en-us/library/system.collections.hashtable%28v=vs.110%29.aspx

Represents a collection of key/value pairs that are organized based on the hash code of the key

A key cannot be null, but a value can be

The objects used as keys by a Hashtable are required to override the Object.GetHashCode and the Object.Equals methods = You need to provide hash implementation

Iteration returns a DictionaryEntry object

HashSet<T>

https://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx

It contains no duplicate elements

Elements are stored in no particular order

It is like a Dictionary<TKey, TValue> , but in a HashSet the value is the Key

Use to group unique values, to perform fast add and check operations

Stack<T>

https://msdn.microsoft.com/en-us/library/3278tedw%28v=vs.110%29.aspx

Last In First Out (LIFO) collection

Accepts null value, allows duplicate elements

Used to access the information in reverse order

Main operations : Push, Pop, Peek

Use to implement LIFO behavior

Queue<T>

https://msdn.microsoft.com/en-us/library/7977ey2c%28v=vs.110%29.aspx

First In First Out (FIFO) collection

Accepts null value and allows duplicate object

Objects are inserted at one end and removed from the other

Main operations : Enqueue, Dequeue, Peek

Use to implement FIFO behavior

Intro to Linq

(Language-Integrated Query)

Linq

Standard patterns for querying and updating data

Linq to Objects : to query collection of objects

Linq to XML : to query xml documents

Linq to ADO.Net : to query data sets, entities or sql databases (Linq to SQL)

Linq is a set of extension methods that helps developers query and manipulate collections

Linq to Objects queries

var query = from Student student in arrList
            where student.Scores[0] > 95
            orderby student.Scores[0]
            select student;
var query = arrList.Where(x => x.Scores[0] > 95).OrderBy(x => x.Scores[0]);

Lambda expression query (method syntax)

SQL like query (query syntax)

https://msdn.microsoft.com/en-us/library/bb397937.aspx

Sources and links

  • About the big O notation : blog post
  • http://bigocheatsheet.com/#data-structures
  • https://msdn.microsoft.com/en-us/default.aspx
  • https://msdn.microsoft.com/en-us/library/ms379570%28v=vs.80%29.aspx
  • https://msdn.microsoft.com/en-us/library/ms379571%28v=vs.80%29.aspx
  • http://www.studytonight.com/data-structures/time-complexity-of-algorithms
  • http://stackoverflow.com/questions/169973/when-should-i-use-a-list-vs-a-linkedlist

Made with Slides.com