KeyValuePair

Posted on 2010-01-08 18:43  saysay,looklook  阅读(940)  评论(0编辑  收藏  举报

NET Framework 类库
KeyValuePair 泛型结构
注意:此结构在 .NET Framework 2.0 版中是新增的。

定义可设置或检索的键/值对。

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

类型参数
TKey
键的类型。

TValue
值的类型。
备注
Dictionary.Enumerator.Current 属性返回此类型的实例。

C# 语言中的 foreach 语句(在 C++ 中为 for each,在 Visual Basic 中为 For Each)需要集合中的元素类

型。由于基于 IDictionary 的集合中的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值

的类型。而是 KeyValuePair 类型。例如:


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

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

Copyright © 2024 saysay,looklook
Powered by .NET 8.0 on Kubernetes