Home  >  Article  >  Backend Development  >  What is the difference between a dictionary and an array in C#?

What is the difference between a dictionary and an array in C#?

王林
王林forward
2023-08-30 13:13:021391browse

C# 中字典和数组有什么区别?

Dictionary

A dictionary is a collection of key-value pairs in C#. Dictionaries are contained in the System.Collection.Generics namespace.

The method of declaring a dictionary is as follows:

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 corresponds to the last element.

To define an array -

int[] arr = new int[5];

Initialize and set array elements.

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

The above is the detailed content of What is the difference between a dictionary and an array in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete