c#中KeyValuePair的用法
KeyValuePair可以这样实例化
KeyValuePair<string, int> mcmillan = new KeyValuePair<string, int>("McMillan", 99);
取值
Console.Write(mcmillan.Key);
Console.Write(" " + mcmillan.Value);
以下是一个把对象放到数组中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataStruct9
{
class Program
{
static void Main(string[] args)
{
KeyValuePair<string, int>[] gradeBook = new KeyValuePair<string, int>[10];
gradeBook[0] = new KeyValuePair<string, int>("McMillan", 99);
gradeBook[1] = new KeyValuePair<string, int>("Ruff", 64);
for (int i = 0; i <= gradeBook.GetUpperBound(0); i++)
if (gradeBook[i].Value != 0)
Console.WriteLine(gradeBook[i].Key + ": " + gradeBook[i].Value);
Console.Read();
}
}
}
原创文章转载请注明出处:云飞扬IT的blog





