• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#中Dictionary类使用实例

C#中Dictionary类使用实例

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了马桶c的个人空间,c语言,欲情 c max,维生素c,crh2c等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

在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; }
  }
}
</div>

实例中需要定义两个类:
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字典并输出,最终的运行效果如下图所示:

图1 程序运行效果图

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

1、Add方法

使用Add方法将Key-Value对加入字典。

2、ContainsKey方法

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

3、TryGetValue方法

使用TryGetValue方法获取指定Key对应的Value。

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

好了,就到这里了。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#利用ReportViewer生成报表
  • C#基于正则去掉注释的方法示例
  • C#中new的用法及与override的区别分析
  • C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • C# for循环的经典案例集锦
  • C#操作word的方法示例
  • C#使用WebClient登录网站并抓取登录后的网页信息实现方法
  • C# WinForm制作异形窗体与控件的方法
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • C#使用NPOI上传excel

相关文章

  • 2017-05-28C#封装的Sqlite访问类实例
  • 2017-05-28C#生成防伪码的思路及源码分享
  • 2017-05-28在Unity中实现动画的正反播放代码
  • 2017-05-28利用windows性能计数器进行服务器性能监控示例分享
  • 2017-05-28c#3.0实现延迟赋值示例
  • 2017-05-28深入解析C#中的泛型类与泛型接口
  • 2017-05-28C#中构造函数和析构函数用法实例详解
  • 2017-05-28C#数值转换-隐式数值转换表参考
  • 2017-05-28C#判断指定驱动器是否是Fat分区格式的方法
  • 2017-05-28c#只读字段和常量的区别,以及静态构造函数的使用实例

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • c#基础学习之封装
    • C#使用NPOI导入Excel的方法详解
    • C#给Excel添加水印实例详解
    • 动态改变gridview列宽度函数分享
    • C#中关于可空类型的小知识
    • 利用WCF双工模式实现即时通讯
    • C#读取XML中元素和属性值的实现代码
    • C#常用正则大全分享
    • C#中实现输入汉字获取其拼音(汉字转拼音)的2种方法
    • c# 引用类型和值类型

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有