ExtraTopLevelOperators.dict<'Key,'Value> 函数 (F#)

根据键/值对的序列生成只读查找表。使用泛型哈希和相等比较给键对象编制索引。

命名空间/模块路径:Microsoft.FSharp.Core.ExtraTopLevelOperators

程序集:FSharp.Core(在 FSharp.Core.dll 中)

// Signature:
dict : seq<'Key * 'Value> -> IDictionary<'Key,'Value> (requires equality)

// Usage:
dict keyValuePairs

参数

  • keyValuePairs
    类型:seq<'Key * 'Value>

返回值

一个实现表示给定集合的 IDictionary<TKey, TValue> 的对象。

备注

此函数在编译的程序集中名为 CreateDictionary。如果从 F# 以外的语言中访问函数,或通过反射访问成员,请使用此名称。

示例

下面的代码示例演示 dict 函数的用法。

open System
open System.Collections.Generic

let seq1 = seq { for i in 1..10 -> i, i*i }
let dictionary1 = dict seq1
if dictionary1.IsReadOnly then
    Console.WriteLine("The dictionary is read only.")
// The type is a read only IDictionary.
// If you try to add or remove elements,
// NotSupportedException is generated, as in the following line:
//dictionary1.Add(new KeyValuePair<int, int>(0, 0))
// You can use read-only methods as in the following lines.
if dictionary1.ContainsKey(5) then
    Console.WriteLine("Value for key 5: {0}", dictionary1.Item(5))
for elem in dictionary1 do
   Console.WriteLine("Key: {0} Value: {1}", elem.Key, elem.Value) 

输出如下所示。

  
  

平台

Windows 8,Windows 7,Windows server 2012中,Windows server 2008 R2

版本信息

F#核心库版本

支持:2.0,4.0,可移植

请参见

参考

Core.ExtraTopLevelOperators 模块 (F#)

Microsoft.FSharp.Core 命名空间 (F#)