首页 > c# > c#中KeyValuePair的用法

c#中KeyValuePair的用法

2009年11月13日 云飞扬 发表评论 阅读评论

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

本文链接: http://www.ajaxcn.net/archives/557

分类: c# 标签: ,
一键分享到:新浪微博分享  分享到网易微博    转贴到开心网  推荐到豆瓣  分享到QQ空间    RSS订阅
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.