什么是列表℃之间的差额;>和字典<>在C# [英] what is the difference between list<> and dictionary<> in c#

查看:203
本文介绍了什么是列表℃之间的差额;>和字典<>在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于列表和字典在C#中一个奇怪的疑问

I have a strange doubt regarding list and dictionary in c#

在列表中,我们添加项目通过下面的方法

In a list we add items to list by using the following method

using System.Collections.Generic;

class Program
{
    static void Main()
    {
       List<int> list = new List<int>();
       list.Add(2);
       list.Add(3);
       list.Add(5);
       list.Add(7);
    }
}

在一本字典,我们将项目添加这样的...

In a dictionary we add items like this ...

using System;
using System.Collections.Generic;

class Program
{
   static void Main()
   {
      Dictionary<string, int> d = new Dictionary<string, int>();
      d.Add("cat", 2);
      d.Add("dog", 1);
      d.Add("llama", 0);
      d.Add("iguana", -1);
   }
}

我不知道到底有什么区别,但在字典中我们添加的东西,如一个(键,值)对,并在列表中,我们只需要添加的项目不指定任何键​​..

I don't know exactly what is the difference, but in a dictionary we add items like a (key,value) pair and in a list we just add items without specifying any key ..

会有人澄清这一点?

推荐答案

的IDictionary 是键盘>值映射,的ICollection 是集相似的对象。

IDictionary is for key->value maps, ICollection is for sets of similar objects.

的ICollection 是相似对象的集合接口:一个窗体上的控件,在列表中的元素,在XML标记的属性,等等。作为.NET 2.0,有一个通用版本,所以你可以参考整数的集合为的ICollection&LT; INT&GT;

ICollection is an interface for collections of similar objects: the controls on a form, the elements in a list, the attributes in an XML tag, and so on. As of .NET 2.0, there's a generic version, so you can refer to a collection of integers as ICollection<int>.

IDictionary的是用于映射一种类型的对象或值到另一个的接口。它像一个真正的字典或电话簿:你心里有一个钥匙就像一个人的名字,当你看到它,你会得到多数民众赞成通过该密钥识别一些信息,如地址或电话号码。每个密钥只能列出一次,虽然两个不同的密钥仍然允许具有相同的值。这也是通用在.NET 2.0,所以一本字典,它的键是字符串,其值是整数,将的IDictionary&LT;字符串,INT和GT;

IDictionary is an interface for mapping one type of object or value to another. It works like a real dictionary, or a phone book: you have a "key" in mind like a person’s name, and when you look it up, you get some information that’s identified by that key, like an address or phone number. Each key can only be listed once, although two different keys are still allowed to have the same value. This is also generic in .NET 2.0, so a dictionary whose keys are strings and whose values are integers would be IDictionary<string,int>.

字典实际上是键/值对的集合:您可以使用的IDictionary&LT; INT,字符串&GT; 的ICollection&LT; KeyValuePair&LT;整型,字符串&GT;&GT; ,您可以访问的键和值作为单独的收藏与键和值的属性。

A dictionary is actually a collection of key/value pairs: you can use an IDictionary<int,string> as an ICollection<KeyValuePair<int,string>>, and you can access the keys and values as separate collections with the Keys and Values properties.

两个的ICollection 的IDictionary 是无序的,这意味着尽管你可以检索的元素以某种顺序与 CopyTo从方法或者一个foreach循环,顺序没有特殊意义,它可能会改变没有明显的原因。这间的ICollection 的主要区别和的IList :名单可以让你把项目的具体位置,就像一个数组,他们呆在那里,直到您移动它们。

Both ICollection and IDictionary are unordered, meaning that although you can retrieve the elements in some order with the CopyTo method or a foreach loop, that order has no special meaning, and it might change for no apparent reason. That’s the main difference between ICollection and IList: a list lets you put items in specific positions, just like an array, and they stay there until you move them.

这篇关于什么是列表℃之间的差额;&GT;和字典&LT;&GT;在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆