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

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

王林
Release: 2023-08-27 15:57:10
forward
1348 people have browsed it

C# 中列表和字典有什么区别?

A dictionary is a collection of keys and values ​​in C#. Dictionary is contained in the System.Collection.Generics namespace. Dictionary is a generic type and will return an error if you try to find a key that does not exist.

List collection is a generic class that can store any data type to create a list.

A list is a set of items -
List<string> myList = new List<string>() {
   "Maths",
   "English",
"   Science"
};
Copy after login

A dictionary is a set of key-value pairs.

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

d.Add("squash", 1);
d.Add("football", 2);
d.Add("rugby", 3);
Copy after login

Looping through lists is easier and faster, and elements can be accessed easily using indexes through the list.

The above is the detailed content of What is the difference between a list and a dictionary 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