C#中Dictionary類應用實例。本站提示廣大學習愛好者:(C#中Dictionary類應用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是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字典並輸入,終究的運轉後果以下圖所示:

圖1 法式運轉後果圖
在我們的實例中應用到了Dictionary類的以下辦法:
1、Add辦法
應用Add辦法將Key-Value對參加字典。
2、ContainsKey辦法
應用ContainsKey辦法可以確認字典中能否包括指定Key的鍵值對,若存在,前往true,不然前往false。
3、TryGetValue辦法
應用TryGetValue辦法獲得指定Key對應的Value。
別的,實例中還應用到了Dictionary類的Keys屬性,該屬性前往字典中一切Key的聚集。
好了,就到這裡了。