zoukankan      html  css  js  c++  java
  • Dictionary 泛型类

    表示键和值的集合。

    命名空间:System.Collections.Generic
    程序集:mscorlib(在 mscorlib.dll 中)

    [SerializableAttribute]

    [ComVisibleAttribute(false)]

    public class Dictionary<TKey,TValue> : IDictionary<TKey,TValue>, ICollection<KeyValuePair<TKey,TValue>>,

            IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection, IEnumerable,

            ISerializable, IDeserializationCallback

    类型参数

    TKey

    字典中的键的类型。

    TValue

    字典中的值的类型。

    Dictionary 泛型类提供了从一组键到一组值的映射。字典中的每个添加项都由一个值及其相关联的键组成。通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary 类是作为一个哈希表来实现的。

    检索速度取决于为 TKey 指定的类型的哈希算法的质量。

    只要对象用作 Dictionary 中的键,它就不能以任何影响其哈希值的方式更改。使用字典的相等比较器比较时,Dictionary 中的任何键都必须是唯一的。键不能为 空引用(在 Visual Basic 中为 Nothing),但是如果值类型 TValue 为引用类型,该值则可以为空。

    Dictionary 需要一个相等实现来确定键是否相等。可以使用一个接受 comparer 参数的构造函数来指定 IEqualityComparer 泛型接口的实现;如果不指定实现,则使用默认的泛型相等比较器 EqualityComparer.Default。如果类型 TKey 实现 System.IEquatable 泛型接口,则默认相等比较器会使用该实现。

    例如,您可以使用 StringComparer 类提供的不区分大小写的字符串比较器来创建带不区分大小写的字符串键的字典。

    Dictionary 的容量是 Dictionary 可以包含的元素数。在此实现中,Dictionary 的默认初始容量为 3;但该默认值可能在 .NET Framework 的未来版本中更改。当向 Dictionary 中添加元素时,将通过重新分配内部数组来根据需要自动增大容量。

    对于枚举而言,字典中的每一项都被视为一个表示值及其键的 KeyValuePair 结构进行处理。项返回的顺序未定义。

    C# 语言的 foreach 语句(在 C++ 中为 for each,在 Visual Basic 中为 For Each)需要集合中每个元素的类型。由于 Dictionary 是键和值的集合,因此元素类型并非键类型或值类型。相反,元素类型是键类型和值类型的 KeyValuePair。例如:

    foreach (KeyValuePair<int, string> kvp in myDictionary) {...}

    foreach 语句是对枚举数的包装,只允许该枚举数读取集合,而不允许写入集合。

    示例:

    C#

    using System;

    using System.Collections.Generic;

     

    public class Example

    {

        public static void Main()

        {

            // Create a new dictionary of strings, with string keys.

            //

            Dictionary<string, string> openWith =

                new Dictionary<string, string>();

     

            // Add some elements to the dictionary. There are no

            // duplicate keys, but some of the values are duplicates.

            openWith.Add("txt", "notepad.exe");

            openWith.Add("bmp", "paint.exe");

            openWith.Add("dib", "paint.exe");

            openWith.Add("rtf", "wordpad.exe");

     

            // The Add method throws an exception if the new key is

            // already in the dictionary.

            try

            {

                openWith.Add("txt", "winword.exe");

            }

            catch (ArgumentException)

            {

                Console.WriteLine("An element with Key = \"txt\" already exists.");

            }

     

            // The Item property is another name for the indexer, so you

            // can omit its name when accessing elements.

            Console.WriteLine("For key = \"rtf\", value = {0}.",

                openWith["rtf"]);

     

            // The indexer can be used to change the value associated

            // with a key.

            openWith["rtf"] = "winword.exe";

            Console.WriteLine("For key = \"rtf\", value = {0}.",

                openWith["rtf"]);

     

            // If a key does not exist, setting the indexer for that key

            // adds a new key/value pair.

            openWith["doc"] = "winword.exe";

     

            // The indexer throws an exception if the requested key is

            // not in the dictionary.

            try

            {

                Console.WriteLine("For key = \"tif\", value = {0}.",

                    openWith["tif"]);

            }

            catch (KeyNotFoundException)

            {

                Console.WriteLine("Key = \"tif\" is not found.");

            }

     

            // When a program often has to try keys that turn out not to

            // be in the dictionary, TryGetValue can be a more efficient

            // way to retrieve values.

            string value = "";

            if (openWith.TryGetValue("tif", out value))

            {

                Console.WriteLine("For key = \"tif\", value = {0}.", value);

            }

            else

            {

                Console.WriteLine("Key = \"tif\" is not found.");

            }

     

            // ContainsKey can be used to test keys before inserting

            // them.

            if (!openWith.ContainsKey("ht"))

            {

                openWith.Add("ht", "hypertrm.exe");

                Console.WriteLine("Value added for key = \"ht\": {0}",

                    openWith["ht"]);

            }

     

            // When you use foreach to enumerate dictionary elements,

            // the elements are retrieved as KeyValuePair objects.

            Console.WriteLine();

            foreach( KeyValuePair<string, string> kvp in openWith )

            {

                Console.WriteLine("Key = {0}, Value = {1}",

                    kvp.Key, kvp.Value);

            }

     

            // To get the values alone, use the Values property.

            Dictionary<string, string>.ValueCollection valueColl =

                openWith.Values;

     

            // The elements of the ValueCollection are strongly typed

            // with the type that was specified for dictionary values.

            Console.WriteLine();

            foreach( string s in valueColl )

            {

                Console.WriteLine("Value = {0}", s);

            }

     

            // To get the keys alone, use the Keys property.

            Dictionary<string, string>.KeyCollection keyColl =

                openWith.Keys;

     

            // The elements of the KeyCollection are strongly typed

            // with the type that was specified for dictionary keys.

            Console.WriteLine();

            foreach( string s in keyColl )

            {

                Console.WriteLine("Key = {0}", s);

            }

     

            // Use the Remove method to remove a key/value pair.

            Console.WriteLine("\nRemove(\"doc\")");

            openWith.Remove("doc");

     

            if (!openWith.ContainsKey("doc"))

            {

                Console.WriteLine("Key \"doc\" is not found.");

            }

        }

    }

     

    /* This code example produces the following output:

     

    An element with Key = "txt" already exists.

    For key = "rtf", value = wordpad.exe.

    For key = "rtf", value = winword.exe.

    Key = "tif" is not found.

    Key = "tif" is not found.

    Value added for key = "ht": hypertrm.exe

     

    Key = txt, Value = notepad.exe

    Key = bmp, Value = paint.exe

    Key = dib, Value = paint.exe

    Key = rtf, Value = winword.exe

    Key = doc, Value = winword.exe

    Key = ht, Value = hypertrm.exe

     

    Value = notepad.exe

    Value = paint.exe

    Value = paint.exe

    Value = winword.exe

    Value = winword.exe

    Value = hypertrm.exe

     

    Key = txt

    Key = bmp

    Key = dib

    Key = rtf

    Key = doc

    Key = ht

     

    Remove("doc")

    Key "doc" is not found.

     */

  • 相关阅读:
    C#中async/await中的异常处理
    Xunit
    Markdown安装与简单使用
    .Net Ajax跨域请求总结
    centos 安装 Vmare tool
    linux安装Java
    linux常用命令整理
    autofac解析Mvc和Webapi的坑
    swarm on ubuntu
    deploy service on swarm
  • 原文地址:https://www.cnblogs.com/gsk99/p/1904575.html
Copyright © 2011-2022 走看看