C#中Dictionary類使用實(shí)例
在C#中,使用Dictionary類來管理由鍵值對(duì)組成的集合,這類集合稱為字典。
字典最大的特點(diǎn)就是能夠根據(jù)鍵來快速查找集合中的值。
下面是一個(gè)使用字典的小實(shí)例,希望通過這個(gè)小實(shí)例,能讓大家對(duì)字典操作有一個(gè)初步的了解。下面是完整代碼。
//************************************************************
//
// Dictionary示例代碼
//
// Author:三五月兒
//
// Date:2014/09/14
//
//
//************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
namespace DictionaryExp
{
class Program
{
static void Main(string[] args)
{
//所有班級(jí)所有學(xué)生成績(jī)報(bào)告單
Dictionary<int, List<ScoreReport>> scoreDictionary = new Dictionary<int, List<ScoreReport>>();
//將1班所有學(xué)生成績(jī)加入字典
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班所有學(xué)生的成績(jī)加入字典
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班所有學(xué)生的成績(jī)加入字典
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}
});
//所有班級(jí)學(xué)生成績(jī)統(tǒng)計(jì)報(bào)告單
Dictionary<int, ScoreStatistics> scoreStatisticsDictionary = new Dictionary<int, ScoreStatistics>();
scoreStatisticsDictionary.Add(1, new ScoreStatistics());
scoreStatisticsDictionary.Add(2, new ScoreStatistics());
scoreStatisticsDictionary.Add(3, new ScoreStatistics());
//獲取班級(jí)Key的集合
Dictionary<int, List<ScoreReport>>.KeyCollection keyCollection = scoreDictionary.Keys;
//通過班級(jí)Key遍歷班級(jí)學(xué)生成績(jī)
foreach (var key in keyCollection)
{
//班級(jí)成績(jī)統(tǒng)計(jì)報(bào)告單中包含本班級(jí)時(shí)才繼續(xù)
if (scoreStatisticsDictionary.ContainsKey(key))
{
//當(dāng)前班級(jí)所有學(xué)生的詳細(xì)成績(jī)報(bào)告單
List<ScoreReport> scoreList = new List<ScoreReport>();
scoreDictionary.TryGetValue(key, out scoreList);
if (scoreList != null && scoreList.Count > 0)
{//當(dāng)前班級(jí)所有學(xué)生的詳細(xì)成績(jī)報(bào)告單中存在數(shù)據(jù)
int count = scoreList.Count;//當(dāng)前班級(jí)學(xué)生人數(shù)
//生成當(dāng)前班級(jí)學(xué)生成績(jī)的統(tǒng)計(jì)報(bào)告單
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; }
}
}
實(shí)例中需要定義兩個(gè)類:
ScoreReport類表示單個(gè)學(xué)生的成績(jī)報(bào)告單,而ScoreStatistics類表示整個(gè)班級(jí)的成績(jī)統(tǒng)計(jì)報(bào)告單,統(tǒng)計(jì)信息包含班級(jí)各科成績(jī)的總分和平均分。
在程序的Main方法中:
首先,實(shí)例化字典對(duì)象,其中:
Dictionary<int, List<ScoreReport>>類型的字典scoreDictionary用來保存所有班級(jí)所有學(xué)生的詳細(xì)成績(jī)報(bào)告單,Key為班級(jí)Id,Value為L(zhǎng)ist<ScoreReport>類型的集合,該集合保存班級(jí)所有學(xué)生的成績(jī)報(bào)告單。
Dictionary<int, ScoreStatistics>類型的字典scoreStatisticsDictionary用來保存所有班級(jí)的成績(jī)統(tǒng)計(jì)報(bào)告單,Key同樣為班級(jí)Id,Value為班級(jí)的成績(jī)統(tǒng)計(jì)報(bào)告單,使用ScoreStatistics類型的對(duì)象保存。
接著,向字典scoreDictionary與scoreStatisticsDictionary中分別加入三個(gè)班級(jí)的學(xué)生詳細(xì)成績(jī)報(bào)告單及班級(jí)成績(jī)統(tǒng)計(jì)報(bào)告單,此時(shí)scoreStatisticsDictionary中加入的班級(jí)成績(jī)統(tǒng)計(jì)報(bào)告單并不包含真實(shí)的統(tǒng)計(jì)信息,真實(shí)的統(tǒng)計(jì)信息需要在后面的計(jì)算過程中寫入。
最后,遍歷scoreDictionary字典,依次取出各個(gè)班級(jí)所有學(xué)生的成績(jī)信息并生成班級(jí)成績(jī)的統(tǒng)計(jì)信息寫入scoreDictionary字典并輸出,最終的運(yùn)行效果如下圖所示:

圖1 程序運(yùn)行效果圖
在我們的實(shí)例中使用到了Dictionary類的以下方法:
1、Add方法
使用Add方法將Key-Value對(duì)加入字典。
2、ContainsKey方法
使用ContainsKey方法可以確認(rèn)字典中是否包含指定Key的鍵值對(duì),若存在,返回true,否則返回false。
3、TryGetValue方法
使用TryGetValue方法獲取指定Key對(duì)應(yīng)的Value。
另外,實(shí)例中還使用到了Dictionary類的Keys屬性,該屬性返回字典中所有Key的集合。
好了,就到這里了。
- Lua Table轉(zhuǎn)C# Dictionary的方法示例
- C#中數(shù)組、ArrayList、List、Dictionary的用法與區(qū)別淺析(存取數(shù)據(jù))
- C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題
- C#常見的幾種集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍歷方法對(duì)比
- C#中Dictionary泛型集合7種常見的用法
- C#中查找Dictionary中的重復(fù)值的方法
- C#實(shí)現(xiàn)自定義Dictionary類實(shí)例
- C#針對(duì)xml文件轉(zhuǎn)化Dictionary的方法
- C#泛型集合Dictionary<K,V>的使用方法
- C#中Dictionary的作用及用法講解
- C#泛型Dictionary的用法實(shí)例詳解
- C#探秘系列(一)——ToDictionary,ToLookup
- C#中查找Dictionary中重復(fù)值的方法
- C# Hashtable/Dictionary寫入和讀取對(duì)比詳解
- C# Dictionary和SortedDictionary的簡(jiǎn)介
相關(guān)文章
C#byte數(shù)組與Image的相互轉(zhuǎn)換實(shí)例代碼
這篇文章主要介紹了C#byte數(shù)組與Image的相互轉(zhuǎn)換實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
Unity游戲開發(fā)中的設(shè)計(jì)模式之策略模式
策略模式是Unity游戲開發(fā)中常用的設(shè)計(jì)模式之一,用于封裝一系列算法或行為,并使這些算法或行為可以相互替換。通過策略模式,可以在運(yùn)行時(shí)動(dòng)態(tài)地選擇算法或行為,實(shí)現(xiàn)游戲中的多樣性和可擴(kuò)展性。常見的應(yīng)用包括AI行為、武器攻擊、移動(dòng)方式等2023-05-05
C#實(shí)現(xiàn)鼠標(biāo)左右鍵切換效果
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)鼠標(biāo)左右鍵切換功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12

