Dictionary
using OnLineGame;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DictionaryDemo
{
class Program
{
static void Main(string[] args)
{
DictionarySample();
Console.ReadLine();
}
static void DictionarySample()
{
//宣告 Dictionary 時候順便加東西
Dictionary<string, Gamer> dictionaryGamers = new Dictionary<string, Gamer>
{
{"Key1", new Gamer {Id = 1, Name = "Name1", Email = "[email protected]"}},
{"Key2", new Gamer {Id = 2, Name = "Name2", Email = "[email protected]"}}
};
//宣告 Dictionary 之後再加東西
dictionaryGamers.Add("Key3", new Gamer { Id = 3, Name = "Name3", Email = "[email protected]" });
dictionaryGamers.Add("Key4", new Gamer { Id = 4, Name = "Name4", Email = "[email protected]" });
Console.WriteLine($"dictionaryGamers.Count = {dictionaryGamers.Count}");
Console.WriteLine($"包含:");
#region Dictionary.FirstOrDefault
KeyValuePair<string, Gamer> key1Value = dictionaryGamers.FirstOrDefault(g => g.Key == "Key1");
Gamer gamer1 = dictionaryGamers.FirstOrDefault(g => g.Key == "Key1").Value;
KeyValuePair<string, Gamer> key2Value = dictionaryGamers.FirstOrDefault(x => x.Key == "Key2");
Console.WriteLine($"key1Value.Key : {key1Value.Key} ; " + $"key1Value.Value.ToString() : {key1Value.Value.ToString()}");
Console.WriteLine($"key2Value.Key : {key2Value.Key} ; " + $"key2Value.Value.ToString() : {key2Value.Value}");
#endregion
#region Dictionary.FirstOrDefault
KeyValuePair<string, Gamer> lastItem = dictionaryGamers.Last();
Console.WriteLine("");
Console.WriteLine("最後一個為:");
Console.WriteLine($"lastItem.Key : {lastItem.Key} ; " + $"lastItem.Value.ToString() : {lastItem.Value}");
#endregion
Console.WriteLine("========================================================================================================================");
Dictionary<string, string> A = new Dictionary<string, string>();
A.Add("ID_1", "Name_1");//Index = 0
A.Add("ID_2", "Name_2");//Index = 1
A.Add("ID_3", "Name_3");//Index = 2
#region Dictionary.ElementAt
Console.WriteLine("");
Console.WriteLine("Dictionary.ElementAt(Dictionary.Count - 1)");
var obj = A.ElementAt(A.Count - 1);
Console.WriteLine($"A{A.Count - 1}.Key = {obj.Key}");
Console.WriteLine($"A{A.Count - 1}.Value = {obj.Value}");
#endregion
#region Dictionary.Keys.ElementAt
Console.WriteLine("");
Console.WriteLine("Dictionary.Keys.ElementAt(Dictionary.Count - 1)");
string temp = A.Keys.ElementAt(A.Count - 1);
Console.WriteLine($"A{A.Count - 1}.Key = {temp}");
Console.WriteLine($"A{A.Count - 1}.Value = {A[temp]}");
#endregion
#region foreach(var item in Dictionary){...}
Console.WriteLine("");
Console.WriteLine("foreach(var item in Dictionary){...}");
foreach (var item in A)
{
Console.WriteLine($"item.Key == {item.Key} ; item.Value : {item.Value}");
}
#endregion
#region Dictionary.ContainsKey
Console.WriteLine("");
Console.WriteLine("Dictionary.ContainsKey(KEY)");
if (!A.ContainsKey("ID_4"))
{
A.Add("ID_4", "Name_4");
}
foreach (var item in A)
{
Console.WriteLine($"item.Key == {item.Key} ; item.Value : {item.Value}");
}
#endregion
#region Dictionary.Remove
Console.WriteLine("");
Console.WriteLine("Dictionary.Remove(KEY)");
A.Remove("ID_4");
foreach (var item in A)
{
Console.WriteLine($"item.Key == {item.Key} ; item.Value : {item.Value}");
}
#endregion
#region Dictionary.Clear()
Console.WriteLine("");
Console.WriteLine("Dictionary.Clear()");
A.Clear();
Console.WriteLine($"A.Count = {A.Count}");
#endregion
Console.WriteLine("");
Console.WriteLine("========================================================================================================================");
Console.WriteLine("ArrayObject.ToDictionary()");
Gamer[] customersArr = {
new Gamer {Id = 1, Name = "Name1", Email = "[email protected]"},
new Gamer {Id = 2, Name = "Name2", Email = "[email protected]"},
new Gamer {Id = 3, Name = "Name3", Email = "[email protected]" },
new Gamer {Id = 4, Name = "Name4", Email = "[email protected]" }
};
#region 方法一
Console.WriteLine("方法一:");
Dictionary<int, Gamer> dictionaryGamers1 = customersArr.ToDictionary(gamerItem => gamerItem.Id//Key
, gamerItem => gamerItem//Vallue
);
foreach (KeyValuePair<int, Gamer> item in dictionaryGamers1)
{
Console.WriteLine($"item.Key == {item.Key} ; item.Value : {item.Value}");
}
#endregion
#region 方法二
Console.WriteLine("");
Console.WriteLine("方法二:");
Dictionary<int, Gamer> dictionaryGamers2 = customersArr.ToDictionary(gamerItem => gamerItem.Id);
foreach (KeyValuePair<int, Gamer> item in dictionaryGamers2)
{
Console.WriteLine($"item.Key == {item.Key} ; item.Value : {item.Value}");
}
#endregion
#region 方法三
Console.WriteLine("");
Console.WriteLine("方法三:(此種方式最常見)");
Dictionary<int, Gamer> dictionaryGamers3 = new Dictionary<int, Gamer>();
foreach (Gamer gamerItem in customersArr)
{
dictionaryGamers3.Add(gamerItem.Id, gamerItem);
}
foreach (KeyValuePair<int, Gamer> item in dictionaryGamers3)
{
Console.WriteLine($"item.Key == {item.Key} ; item.Value : {item.Value}");
}
#endregion
#region 方法四
Console.WriteLine("");
Console.WriteLine("方法四:");
Dictionary<int, Gamer> dictionaryGamers4 = new Dictionary<int, Gamer>();
for (int index = 0; index < customersArr.Length; index++)
{
Gamer gamerItem = customersArr[index];
dictionaryGamers4.Add(gamerItem.Id, gamerItem);
}
foreach (KeyValuePair<int, Gamer> item in dictionaryGamers4)
{
Console.WriteLine($"item.Key == {item.Key} ; item.Value : {item.Value}");
}
#endregion
}
}
}
namespace OnLineGame
{
// 1. ---------------------------------------------
public class Gamer
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public override string ToString()
{
return $"Id == {Id} ; Name == {Name} ; Email : {Email}";
}
}
}