欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中Dictionary类使用实例

程序员文章站 2023-10-24 00:00:24
在c#中,使用dictionary类来管理由键值对组成的集合,这类集合称为字典。 字典最大的特点就是能够根据键来快速查找集合中的值。 下面是一个使用字典的小实例,希望通...

在c#中,使用dictionary类来管理由键值对组成的集合,这类集合称为字典。

字典最大的特点就是能够根据键来快速查找集合中的值。

下面是一个使用字典的小实例,希望通过这个小实例,能让大家对字典操作有一个初步的了解。下面是完整代码。

//************************************************************ 
// 
// dictionary示例代码 
// 
// author:三五月儿 
//  
// date:2014/09/14 
// 
// 
//************************************************************ 
using system;
using system.collections.generic;
using system.linq;
namespace dictionaryexp
{
  class program
  {
    static void main(string[] args)
    {
      //所有班级所有学生成绩报告单
      dictionary<int, list<scorereport>> scoredictionary = new dictionary<int, list<scorereport>>();
      //将1班所有学生成绩加入字典
      scoredictionary.add(1,
        new list<scorereport>()
        {
          new scorereport(){name="三五月儿",chinesescore=100,mathscore=100,englishscore=100},
          new scorereport(){name="张三",chinesescore=80,mathscore=78,englishscore=91},
          new scorereport(){name="李四",chinesescore=90,mathscore=87,englishscore=88}
        });
      //将2班所有学生的成绩加入字典
      scoredictionary.add(2,
        new list<scorereport>()
        {
          new scorereport(){name="王五",chinesescore=78,mathscore=88,englishscore=98},
          new scorereport(){name="丁六",chinesescore=77,mathscore=99,englishscore=91},
          new scorereport(){name="魏源",chinesescore=45,mathscore=66,englishscore=99}
        });
      //将3班所有学生的成绩加入字典
      scoredictionary.add(3,
        new list<scorereport>()
        {
          new scorereport(){name="周鹏",chinesescore=99,mathscore=89,englishscore=78},
          new scorereport(){name="毛钱",chinesescore=66,mathscore=98,englishscore=91},
          new scorereport(){name="皮蛋",chinesescore=87,mathscore=69,englishscore=88}
        });
 
      //所有班级学生成绩统计报告单
      dictionary<int, scorestatistics> scorestatisticsdictionary = new dictionary<int, scorestatistics>();
      scorestatisticsdictionary.add(1, new scorestatistics());
      scorestatisticsdictionary.add(2, new scorestatistics());
      scorestatisticsdictionary.add(3, new scorestatistics());
 
      //获取班级key的集合
      dictionary<int, list<scorereport>>.keycollection keycollection = scoredictionary.keys;
      //通过班级key遍历班级学生成绩
      foreach (var key in keycollection)
      {
        //班级成绩统计报告单中包含本班级时才继续
        if (scorestatisticsdictionary.containskey(key))
        {
          //当前班级所有学生的详细成绩报告单
          list<scorereport> scorelist = new list<scorereport>();
          scoredictionary.trygetvalue(key, out scorelist);          
          if (scorelist != null && scorelist.count > 0)
          {//当前班级所有学生的详细成绩报告单中存在数据
            int count = scorelist.count;//当前班级学生人数
            //生成当前班级学生成绩的统计报告单
            scorestatistics scorestatistics = new scorestatistics();
            scorestatisticsdictionary.trygetvalue(key, out scorestatistics);
            scorestatistics.classid = key;
            scorestatistics.totalchinesescore = scorelist.sum(it => it.chinesescore);
            scorestatistics.totalmathscore = scorelist.sum(it => it.mathscore);
            scorestatistics.totalenglishscore = scorelist.sum(it => it.englishscore);
            scorestatistics.averagechinesescore = scorestatistics.totalchinesescore / count;
            scorestatistics.averagemathscore = scorestatistics.totalmathscore / count;
            scorestatistics.averageenglishscore = scorestatistics.totalenglishscore / count;
          }
        }
      }
 
      foreach (var s in scorestatisticsdictionary)
      {
        console.writeline("classid = {0},totalchinesescore = {1},totalmathscore = {2},totalenglishscore = {3},averagechinesescore = {4},averagemathscore = {5},averageenglishscore = {6}",
          s.value.classid, s.value.totalchinesescore, s.value.totalmathscore, s.value.totalenglishscore, s.value.averagechinesescore, s.value.averagemathscore, s.value.averageenglishscore);
        console.writeline("-------------------------------------------------");
      }
    }
  }
  class scorereport
  {
    public string name { get; set; }
    public int chinesescore { get; set; }
    public int mathscore { get; set; }
    public int englishscore { get; set; }
  }
 
  class scorestatistics
  {
    public int classid { get; set; }
    public int totalchinesescore { get; set; }
    public int totalmathscore { get; set; }
    public int totalenglishscore { get; set; }
    public int averagechinesescore { get; set; }
    public int averagemathscore { get; set; }
    public int averageenglishscore { get; set; }
  }
}

实例中需要定义两个类:
scorereport类表示单个学生的成绩报告单,而scorestatistics类表示整个班级的成绩统计报告单,统计信息包含班级各科成绩的总分和平均分。

在程序的main方法中:

首先,实例化字典对象,其中:

dictionary<int, list<scorereport>>类型的字典scoredictionary用来保存所有班级所有学生的详细成绩报告单,key为班级id,value为list<scorereport>类型的集合,该集合保存班级所有学生的成绩报告单。

dictionary<int, scorestatistics>类型的字典scorestatisticsdictionary用来保存所有班级的成绩统计报告单,key同样为班级id,value为班级的成绩统计报告单,使用scorestatistics类型的对象保存。

接着,向字典scoredictionary与scorestatisticsdictionary中分别加入三个班级的学生详细成绩报告单及班级成绩统计报告单,此时scorestatisticsdictionary中加入的班级成绩统计报告单并不包含真实的统计信息,真实的统计信息需要在后面的计算过程中写入。

最后,遍历scoredictionary字典,依次取出各个班级所有学生的成绩信息并生成班级成绩的统计信息写入scoredictionary字典并输出,最终的运行效果如下图所示:

C#中Dictionary类使用实例

图1 程序运行效果图

在我们的实例中使用到了dictionary类的以下方法:

1、add方法

使用add方法将key-value对加入字典。

2、containskey方法

使用containskey方法可以确认字典中是否包含指定key的键值对,若存在,返回true,否则返回false。

3、trygetvalue方法

使用trygetvalue方法获取指定key对应的value。

另外,实例中还使用到了dictionary类的keys属性,该属性返回字典中所有key的集合。

好了,就到这里了。