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

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

王林
王林forward
2023-08-27 15:57:101317browse

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"
};

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);

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!

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