ရိုးရိုးလေး ရှင်းပြမယ်
Collections (Dictionary) ကို ရိုးရိုးလေးနားလည်မယ်
Dictionary<TKey, TValue> သည် data ကို key-value pair အနေနဲ့သိမ်းတဲ့ collection ပါ။ Key နဲ့ value ကိုတွဲသိမ်းတာကြောင့် username နဲ့ role, product code နဲ့ price, country နဲ့ capital စတဲ့ mapping data တွေအတွက် အသုံးဝင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Unknown key ကိုindexer နဲ့တိုက်ရိုက်ဖတ်ခြင်းက`KeyNotFoundException` ဖြစ်စေပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
`Dictionary<TKey,TValue>` သည်unique key မှvalue lookup ပေးပြီး equality comparer နဲ့hash code correctness ပေါ်မူတည်ပါတယ်။ Missing key, duplicate add, case sensitivity နဲ့mutable keys ကိုသတိထားပါ။
- Task / collection
- .NET API + pipeline
- Typed result / failure
Collections (Dictionary) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> productPrices = new Dictionary<string, int>();
productPrices.Add("Laptop", 1200);
productPrices.Add("Mouse", 25);
productPrices.Add("Keyboard", 80);
Console.WriteLine(productPrices["Laptop"]);
if (productPrices.ContainsKey("Mouse"))
{
Console.WriteLine($"Mouse price: {productPrices["Mouse"]}");
}
}
}`Dictionary<TKey,TValue>` သည်unique key မှvalue lookup ပေးပြီး equality comparer နဲ့hash code correctness ပေါ်မူတည်ပါတယ်။ Missing key, duplicate add, case sensitivity နဲ့mutable keys ကိုသတိထားပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Task / collection
using System;Dictionary<TKey, TValue> သည် data ကို key-value pair အနေနဲ့သိမ်းတဲ့ collection ပါ။ Key နဲ့ value ကိုတွဲသိမ်းတာကြောင့် username နဲ့ role, product code နဲ့ price, country နဲ့ capital စတဲ့ mapping data တွေအတွက် အသုံးဝင်ပါတယ်။
- .NET API + pipeline
productPrices.Add("Mouse", 25);`Dictionary<TKey,TValue>` သည်unique key မှvalue lookup ပေးပြီး equality comparer နဲ့hash code correctness ပေါ်မူတည်ပါတယ်။ Missing key, duplicate add, case sensitivity နဲ့mutable keys ကိုသတိထားပါ။
- Typed result / failure
}Case-insensitive email dictionary ကို`StringComparer.OrdinalIgnoreCase` နဲ့တည်ဆောက်ပြီး `TryGetValue` နဲ့duplicate policy စမ်းပါ။
- Task / collection
- .NET API + pipeline
- Typed result / failure
Success
Collections (Dictionary) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Unknown key ကိုindexer နဲ့တိုက်ရိုက်ဖတ်ခြင်းက`KeyNotFoundException` ဖြစ်စေပါတယ်။
Dictionary<TKey, TValue> သည် data ကို key-value pair အနေနဲ့သိမ်းတဲ့ collection ပါ။ Key နဲ့ value ကိုတွဲသိမ်းတာကြောင့် username နဲ့ role, product code နဲ့ price, country နဲ့ capital စတဲ့ mapping data တွေအတွက် အသုံးဝင်ပါတယ်။
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> productPrices = new Dictionary<string, int>();
productPrices.Add("Laptop", 1200);
productPrices.Add("Mouse", 25);
productPrices.Add("Keyboard", 80);
Console.WriteLine(productPrices["Laptop"]);
if (productPrices.ContainsKey("Mouse"))
{
Console.WriteLine($"Mouse price: {productPrices["Mouse"]}");
}
}
}သတိထားရန်
productPrices["Laptop"]က key ကိုသုံးပြီး value ကိုယူပါတယ်။- မရှိတဲ့ key ကိုတိုက်ရိုက်ခေါ်ရင် error ဖြစ်နိုင်ပါတယ်။ ဒါကြောင့်
ContainsKey()နဲ့အရင်စစ်တာက လုံခြုံပါတယ်။ - Dictionary key တွေက unique ဖြစ်ရပါတယ်။ Key တူကိုထပ်ထည့်ရင် error ဖြစ်နိုင်ပါတယ်။
1200 Mouse price: 25Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Case-insensitive email dictionary ကို`StringComparer.OrdinalIgnoreCase` နဲ့တည်ဆောက်ပြီး `TryGetValue` နဲ့duplicate policy စမ်းပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Case-insensitive email dictionary ကို`StringComparer.OrdinalIgnoreCase` နဲ့တည်ဆောက်ပြီး `TryGetValue` နဲ့duplicate policy စမ်းပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> productPrices = new Dictionary<string, int>();
productPrices.Add("Laptop", 1200);
productPrices.Add("Mouse", 25);
productPrices.Add("Keyboard", 80);
Console.WriteLine(productPrices["Laptop"]);
if (productPrices.ContainsKey("Mouse"))
{
Console.WriteLine($"Mouse price: {productPrices["Mouse"]}");
}
}
}ရလဒ်: Collections (Dictionary) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Case-insensitive email dictionary ကို`StringComparer.OrdinalIgnoreCase` နဲ့တည်ဆောက်ပြီး `TryGetValue` နဲ့duplicate policy စမ်းပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Task / collection → .NET API + pipeline → Typed result / failure
- `Dictionary<TKey,TValue>` သည်unique key မှvalue lookup ပေးပြီး equality comparer နဲ့hash code correctness ပေါ်မူတည်ပါတယ်။ Missing key, duplicate add, case sensitivity နဲ့mutable keys ကိုသတိထားပါ။
- Unknown key ကိုindexer နဲ့တိုက်ရိုက်ဖတ်ခြင်းက`KeyNotFoundException` ဖြစ်စေပါတယ်။
- Case-insensitive email dictionary ကို`StringComparer.OrdinalIgnoreCase` နဲ့တည်ဆောက်ပြီး `TryGetValue` နဲ့duplicate policy စမ်းပါ။
Collections and data structures — Microsoft Learn