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

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

王林
Release: 2023-08-30 13:13:02
forward
1441 people have browsed it

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>();
Copy after login

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);
Copy after login

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];
Copy after login

Initialize and set array elements.

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template