What are the differences between a dictionary and an array in C#?



Dictionary

Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.

To declare a Dictionary −

IDictionary<int, int> d = new Dictionary<int, int>();

To add elements −

IDictionary<int, int> d = new Dictionary<int, int>();
d.Add(1,97);
d.Add(2,89);
d.Add(3,77);
d.Add(4,88);

Array

Array stores a fixed-size sequential collection of elements of the same type. It consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

To define Arrays −

int[] arr = new int[5];

To initialize and set elements to Arrays.

int[] arr = new int[10] {3, 5, 35, 87, 56, 99, 44, 36, 78};
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements