温馨提示×

c# keyvaluepair能取值吗

c#
小樊
126
2024-11-20 06:33:07
栏目: 编程语言

是的,在C#中,KeyValuePair<TKey, TValue> 结构可以获取键值对的值。你可以通过访问 Value 属性来实现这一目标。以下是一个简单的示例:

using System; using System.Collections.Generic; class Program { static void Main() { // 创建一个 KeyValuePair<string, int> 类型的实例 KeyValuePair<string, int> keyValuePair = new KeyValuePair<string, int>("example", 42); // 获取键值对的值 int value = keyValuePair.Value; // 输出值 Console.WriteLine("Value: " + value); } } 

在这个示例中,我们创建了一个 KeyValuePair<string, int> 类型的实例,其中键为 “example”,值为 42。然后我们通过访问 Value 属性获取值并将其输出到控制台。

0