在C#中,你可以使用KeyValuePair<TKey, TValue>类来表示一个键值对。这个类位于System.Collections.Generic命名空间中。下面是一个简单的示例,展示了如何使用KeyValuePair<TKey, TValue>:
using System;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        // 创建一个 KeyValuePair 列表
        List<KeyValuePair<string, int>> keyValuePairs = new List<KeyValuePair<string, int>>();
        // 添加一些键值对到列表中
        keyValuePairs.Add(new KeyValuePair<string, int>("apple", 1));
        keyValuePairs.Add(new KeyValuePair<string, int>("banana", 2));
        keyValuePairs.Add(new KeyValuePair<string, int>("orange", 3));
        // 遍历列表并输出键值对
        foreach (KeyValuePair<string, int> pair in keyValuePairs)
        {
            Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}");
        }
    }
}
在这个示例中,我们创建了一个List<KeyValuePair<string, int>>类型的变量keyValuePairs,用于存储字符串键和整数值的键值对。然后,我们使用Add方法向列表中添加了一些键值对。最后,我们使用foreach循环遍历列表并输出每个键值对的键和值。

 便宜VPS测评
便宜VPS测评










