Dictionary ทำงานในลักษณะของ key, value
- การใช้งาน
Dictionaryเบื้องต้น - การใช้งาน
Dictionaryกับforeach - ทดลองดึง value ด้วย
TryGetValue - ตรวจสอบว่ามี key แล้วหรือยังด้วย
ContainsKey - ดึงเฉพาะ value ทั้งหมดด้วย
ValueCollection - ดึงเฉพาะ key ทั้งหมดด้วย
KeyCollection
1.การใช้งาน Dictionary เบื้องต้น
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> data
= new Dictionary<string, string>();
data.Add("key1", "value1");
data.Add("key2", "value2");
Console.WriteLine(data["key1"]);
Console.WriteLine(data["key2"]);
Console.ReadKey();
}
}
}
// The example displays the following output:
// value1
// value2
2.การใช้งาน Dictionary กับ foreach
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> data
= new Dictionary<string, string>();
data.Add("key1", "value1");
data.Add("key2", "value2");
foreach (string key in data.Keys)
{
Console.WriteLine("{0} : {1}", key, data[key]);
}
Console.ReadKey();
}
}
}
// The example displays the following output:
// key1 : value1
// key2 : value2
.ใช้ KeyValuePair
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> data
= new Dictionary<string, string>();
data.Add("key1", "value1");
data.Add("key2", "value2");
foreach (KeyValuePair<string, string> kvp in data)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
Console.ReadKey();
}
}
}
// The example displays the following output:
// Key = key1, Value = value1
// Key = key2, Value = value2
3.ทดลองดึง value ด้วย TryGetValue
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> data
= new Dictionary<string, string>();
data.Add("key1", "value1");
data.Add("key2", "value2");
string value = "";
if (data.TryGetValue("key1", out value))
{
Console.WriteLine("For key = 'key1', value = {0}.", value);
}
else
{
Console.WriteLine("Key = 'key1' is not found.");
}
if (data.TryGetValue("key3", out value))
{
Console.WriteLine("For key = 'key3', value = {0}.", value);
}
else
{
Console.WriteLine("Key = 'key3' is not found.");
}
Console.ReadKey();
}
}
}
// The example displays the following output:
// For key = 'key1', value = value1.
// Key = 'key3' is not found.
4.ตรวจสอบว่ามี key แล้วหรือยังด้วย ContainsKey
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> data
= new Dictionary<string, string>();
data.Add("key1", "value1");
data.Add("key2", "value2");
// ContainsKey can be used to test keys before inserting them.
if (!data.ContainsKey("key4"))
{
data.Add("key4", "value4");
Console.WriteLine("Value added for key = 'key4': {0}", data["key4"]);
}
foreach (string key in data.Keys)
{
Console.WriteLine("{0} : {1}", key, data[key]);
}
Console.ReadKey();
}
}
}
// The example displays the following output:
// Value added for key = 'key4': value4
// key1 : value1
// key2 : value2
// key4 : value4
5.ดึงเฉพาะ value ทั้งหมดด้วย ValueCollection
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> data
= new Dictionary<string, string>();
data.Add("key1", "value1");
data.Add("key2", "value2");
// To get the values alone, use the Values property.
Dictionary<string, string>.ValueCollection valueColl =
data.Values;
// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
Console.WriteLine();
foreach (string s in valueColl)
{
Console.WriteLine("Value = {0}", s);
}
Console.ReadKey();
}
}
}
// The example displays the following output:
// Value = value1
// Value = value2
6.ดึงเฉพาะ key ทั้งหมดด้วย KeyCollection
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> data
= new Dictionary<string, string>();
data.Add("key1", "value1");
data.Add("key2", "value2");
// To get the keys alone, use the Keys property.
Dictionary<string, string>.KeyCollection keyColl =
data.Keys;
// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
Console.WriteLine();
foreach (string s in keyColl)
{
Console.WriteLine("Key = {0}", s);
}
Console.ReadKey();
}
}
}
// The example displays the following output:
// Key = key1
// Key = key2