Thuta Learning
C#
AdvancedProgrammingအခြေခံ

Collections (Dictionary)

C#သင်ခန်းစာ 2525 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Collections (Dictionary) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Normal, boundary နဲ့invalid cases ကိုကိုယ်တိုင်စမ်းနိုင်ရန်
  • Readable, type-safe နဲ့testable C# code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

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 ကိုသတိထားပါ။

Mental Model
  1. Task / collection
  2. .NET API + pipeline
  3. Typed result / failure

Collections (Dictionary) ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

csharp
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 ကို အဆင့်လိုက်ဖတ်မယ်

  1. Task / collectionusing System;

    Dictionary<TKey, TValue> သည် data ကို key-value pair အနေနဲ့သိမ်းတဲ့ collection ပါ။ Key နဲ့ value ကိုတွဲသိမ်းတာကြောင့် username နဲ့ role, product code နဲ့ price, country နဲ့ capital စတဲ့ mapping data တွေအတွက် အသုံးဝင်ပါတယ်။

  2. .NET API + pipelineproductPrices.Add("Mouse", 25);

    `Dictionary<TKey,TValue>` သည်unique key မှvalue lookup ပေးပြီး equality comparer နဲ့hash code correctness ပေါ်မူတည်ပါတယ်။ Missing key, duplicate add, case sensitivity နဲ့mutable keys ကိုသတိထားပါ။

  3. Typed result / failure}

    Case-insensitive email dictionary ကို`StringComparer.OrdinalIgnoreCase` နဲ့တည်ဆောက်ပြီး `TryGetValue` နဲ့duplicate policy စမ်းပါ။

Execution Flow
  1. Task / collection
  2. .NET API + pipeline
  3. 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 တွေအတွက် အသုံးဝင်ပါတယ်။

csharp
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 ဖြစ်နိုင်ပါတယ်။
You should see
1200 Mouse price: 25

Real-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 ကိုခန့်မှန်းပါ
csharp
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

Collections (Dictionary) ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • 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

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Unknown key ကိုindexer နဲ့တိုက်ရိုက်ဖတ်ခြင်းက`KeyNotFoundException` ဖြစ်စေပါတယ်။
  • Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Case-insensitive email dictionary ကို`StringComparer.OrdinalIgnoreCase` နဲ့တည်ဆောက်ပြီး `TryGetValue` နဲ့duplicate policy စမ်းပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: 1200 Mouse price: 25

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Collections (Dictionary) | Thuta Learning