ရိုးရိုးလေး ရှင်းပြမယ်
Practice Exercises: OOP + Collections + LINQ ကို ရိုးရိုးလေးနားလည်မယ်
ဒီ exercise set က Intermediate/Advanced chapter တွေက OOP concepts (class, inheritance, polymorphism), collections (List, Dictionary) နဲ့ LINQ တို့ကို ပေါင်းစပ်ပြီး လေ့ကျင့်ဖို့ ရည်ရွယ်ပါတယ်။ Task တွေက Practice Exercises: Fundamentals ထက် ပိုခက်ပြီး, class ကိုယ်တိုင် design လုပ်ရတာ၊ collection ကို LINQ နဲ့ query လုပ်ရတာတွေ ပါဝင်ပါတယ်။ ဒီ level ကို ကျော်နိုင်ရင် mini-project တွေကို ပိုမို confident ဖြစ်စွာ ဆောက်နိုင်ပါပြီ။ Task တစ်ခုချင်းစီကို သီးခြား console app project အသစ်ချင်းစီအနေနဲ့ ဆောက်ပြီး run ကြည့်ပါ။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Class/interface များများသုံးတာကိုOOP quality လို့တွက်ခြင်းကover-engineering ဖြစ်စေပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
OOP/collections exercise တွင်responsibility, invariant, interface contract, collection choice နဲ့query execution ကိုဆုံးဖြတ်စေပါ။ Feature behavior ကိုtests မှဖော်ပြပြီး inheritance/composition ရွေးချယ်မှုကိုအကြောင်းပြချက်ပေးပါ။
- လိုအပ်ချက်
- စဉ်းစားပြီး implement
- စမ်းသပ်ပြီးရှင်းပြ
Practice Exercises: OOP + Collections + LINQ ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// Task 1 starter
class Animal
{
public string Name { get; set; }
public virtual void MakeSound()
{
Console.WriteLine($"{Name} makes a sound");
}
}
class Dog : Animal
{
public override void MakeSound() => Console.WriteLine($"{Name} says Woof!");
}
class Cat : Animal
{
public override void MakeSound() => Console.WriteLine($"{Name} says Meow!");
}
// Task 2 starter
Dictionary<string, int> stock = new Dictionary<string, int>
{
{ "Pen", 25 }, { "Notebook", 8 }, { "Eraser", 3 }, { "Ruler", 40 }
};
var lowStock = stock.Where(s => s.Value < 10);
foreach (var item in lowStock)
Console.WriteLine($"{item.Key}: {item.Value}");
// Task 3 starter
class Student
{
public string Name { get; set; }
public List<int> Scores { get; set; }
}
List<Student> students = new List<Student>
{
new Student { Name = "Aye", Scores = new List<int> { 80, 90, 70 } },
new Student { Name = "Min", Scores = new List<int> { 60, 75, 65 } }
};
// students.OrderByDescending(s => s.Scores.Average()) သုံးပြီး top student ကို ရှာပါOOP/collections exercise တွင်responsibility, invariant, interface contract, collection choice နဲ့query execution ကိုဆုံးဖြတ်စေပါ။ Feature behavior ကိုtests မှဖော်ပြပြီး inheritance/composition ရွေးချယ်မှုကိုအကြောင်းပြချက်ပေးပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- လိုအပ်ချက်
// Task 1 starterဒီ exercise set က Intermediate/Advanced chapter တွေက OOP concepts (class, inheritance, polymorphism), collections (List, Dictionary) နဲ့ LINQ တို့ကို ပေါင်းစပ်ပြီး လေ့ကျင့်ဖို့ ရည်ရွယ်ပါတယ်။ Task တွေက Practice Exercises: Fundamentals ထက် ပိုခက်ပြီး, class ကိုယ်တိုင် design လုပ်ရတာ၊ collection ကို LINQ နဲ့ query လုပ်ရတာတွေ ပါဝင်ပါတယ်။ ဒီ level ကို ကျော်နိုင်ရင် mini-project တွေကို ပိုမို confident ဖြစ်စွာ ဆောက်နိုင်ပါပြီ။ Task တစ်ခုချင်းစီကို သီးခြား console app project အသစ်ချင်းစီအနေနဲ့ ဆောက်ပြီး run ကြည့်ပါ။
- စဉ်းစားပြီး implement
Dictionary<string, int> stock = new Dictionary<string...OOP/collections exercise တွင်responsibility, invariant, interface contract, collection choice နဲ့query execution ကိုဆုံးဖြတ်စေပါ။ Feature behavior ကိုtests မှဖော်ပြပြီး inheritance/composition ရွေးချယ်မှုကိုအကြောင်းပြချက်ပေးပါ။
- စမ်းသပ်ပြီးရှင်းပြ
// students.OrderByDescending(s => s.Scores.Average()...Inventory system ကိုProduct entity, repository interface, dictionary lookup နဲ့LINQ low-stock report ဖြင့်တည်ဆောက်ပြီး duplicate, missing နဲ့negative stock tests ထည့်ပါ။
- လိုအပ်ချက်
- စဉ်းစားပြီး implement
- စမ်းသပ်ပြီးရှင်းပြ
Success
Practice Exercises: OOP + Collections + LINQ ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Class/interface များများသုံးတာကိုOOP quality လို့တွက်ခြင်းကover-engineering ဖြစ်စေပါတယ်။
ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
ဒီ exercise set က Intermediate/Advanced chapter တွေက OOP concepts (class, inheritance, polymorphism), collections (List, Dictionary) နဲ့ LINQ တို့ကို ပေါင်းစပ်ပြီး လေ့ကျင့်ဖို့ ရည်ရွယ်ပါတယ်။ Task တွေက Practice Exercises: Fundamentals ထက် ပိုခက်ပြီး, class ကိုယ်တိုင် design လုပ်ရတာ၊ collection ကို LINQ နဲ့ query လုပ်ရတာတွေ ပါဝင်ပါတယ်။ ဒီ level ကို ကျော်နိုင်ရင် mini-project တွေကို ပိုမို confident ဖြစ်စွာ ဆောက်နိုင်ပါပြီ။ Task တစ်ခုချင်းစီကို သီးခြား console app project အသစ်ချင်းစီအနေနဲ့ ဆောက်ပြီး run ကြည့်ပါ။
လေ့ကျင့်ခန်းများ
(1) Animal base class ကို Name property နဲ့ virtual MakeSound() method နဲ့ ရေးပြီး, Dog နဲ့ Cat subclasses နှစ်ခုနဲ့ MakeSound() ကို override လုပ်ပါ။ List<Animal> ထဲမှာ Dog/Cat objects တွေ ထည့်ပြီး loop နဲ့ MakeSound() ခေါ်ကာ polymorphism အလုပ်လုပ်ပုံကို confirm ပါ။ (2) Dictionary<string, int> တစ်ခုနဲ့ product name ကို key, stock quantity ကို value အနေနဲ့ ထားပြီး, stock 10 အောက်ရှိတဲ့ products အားလုံးကို LINQ Where() နဲ့ ရှာပြီး print ပါ။ (3) Student class (Name, List<int> Scores) တစ်ခု ရေးပြီး, List<Student> ထဲက student တစ်ယောက်ချင်းစီရဲ့ average score ကို LINQ Average() သုံးပြီး တွက်ကာ, average အများဆုံး student ကို OrderByDescending() နဲ့ ရှာထုတ်ပါ။
Code နမူနာ
// Task 1 starter
class Animal
{
public string Name { get; set; }
public virtual void MakeSound()
{
Console.WriteLine($"{Name} makes a sound");
}
}
class Dog : Animal
{
public override void MakeSound() => Console.WriteLine($"{Name} says Woof!");
}
class Cat : Animal
{
public override void MakeSound() => Console.WriteLine($"{Name} says Meow!");
}
// Task 2 starter
Dictionary<string, int> stock = new Dictionary<string, int>
{
{ "Pen", 25 }, { "Notebook", 8 }, { "Eraser", 3 }, { "Ruler", 40 }
};
var lowStock = stock.Where(s => s.Value < 10);
foreach (var item in lowStock)
Console.WriteLine($"{item.Key}: {item.Value}");
// Task 3 starter
class Student
{
public string Name { get; set; }
public List<int> Scores { get; set; }
}
List<Student> students = new List<Student>
{
new Student { Name = "Aye", Scores = new List<int> { 80, 90, 70 } },
new Student { Name = "Min", Scores = new List<int> { 60, 75, 65 } }
};
// students.OrderByDescending(s => s.Scores.Average()) သုံးပြီး top student ကို ရှာပါTask သုံးခုစလုံးအတွက် Dog/Cat sound အမှန်ပေါ်ခြင်း၊ low-stock products list ခြင်း၊ average score အများဆုံး student ကို ဖော်ထုတ်ခြင်း ဆိုတဲ့ output များ ရရှိမှာပါ။၅ မိနစ် စမ်းကြည့်
Task 1 ထဲကို Bird class တစ်ခု ထပ်ဖြည့်ပြီး MakeSound() ကို override လုပ်ကာ, List<Animal> ထဲမှာ Bird object ပါအောင် ထည့်ပြီး loop ပြန်run ကြည့်ပါ (5 minutes).
သတိလေးတစ်ချက်
Polymorphism အလုပ်လုပ်ဖို့ base class method ကို virtual, derived class method ကို override နှစ်ခုစလုံး ထားဖို့ လိုအပ်တာကို သတိရပါ။
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Inventory system ကိုProduct entity, repository interface, dictionary lookup နဲ့LINQ low-stock report ဖြင့်တည်ဆောက်ပြီး duplicate, missing နဲ့negative stock tests ထည့်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Inventory system ကိုProduct entity, repository interface, dictionary lookup နဲ့LINQ low-stock report ဖြင့်တည်ဆောက်ပြီး duplicate, missing နဲ့negative stock tests ထည့်ပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
// Task 1 starter
class Animal
{
public string Name { get; set; }
public virtual void MakeSound()
{
Console.WriteLine($"{Name} makes a sound");
}
}
class Dog : Animal
{
public override void MakeSound() => Console.WriteLine($"{Name} says Woof!");
}
class Cat : Animal
{
public override void MakeSound() => Console.WriteLine($"{Name} says Meow!");
}
// Task 2 starter
Dictionary<string, int> stock = new Dictionary<string, int>
{
{ "Pen", 25 }, { "Notebook", 8 }, { "Eraser", 3 }, { "Ruler", 40 }
};
var lowStock = stock.Where(s => s.Value < 10);
foreach (var item in lowStock)
Console.WriteLine($"{item.Key}: {item.Value}");
// Task 3 starter
class Student
{
public string Name { get; set; }
public List<int> Scores { get; set; }
}
List<Student> students = new List<Student>
{
new Student { Name = "Aye", Scores = new List<int> { 80, 90, 70 } },
new Student { Name = "Min", Scores = new List<int> { 60, 75, 65 } }
};
// students.OrderByDescending(s => s.Scores.Average()) သုံးပြီး top student ကို ရှာပါရလဒ်: Practice Exercises: OOP + Collections + LINQ ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Inventory system ကိုProduct entity, repository interface, dictionary lookup နဲ့LINQ low-stock report ဖြင့်တည်ဆောက်ပြီး duplicate, missing နဲ့negative stock tests ထည့်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- လိုအပ်ချက် → စဉ်းစားပြီး implement → စမ်းသပ်ပြီးရှင်းပြ
- OOP/collections exercise တွင်responsibility, invariant, interface contract, collection choice နဲ့query execution ကိုဆုံးဖြတ်စေပါ။ Feature behavior ကိုtests မှဖော်ပြပြီး inheritance/composition ရွေးချယ်မှုကိုအကြောင်းပြချက်ပေးပါ။
- Class/interface များများသုံးတာကိုOOP quality လို့တွက်ခြင်းကover-engineering ဖြစ်စေပါတယ်။
- Inventory system ကိုProduct entity, repository interface, dictionary lookup နဲ့LINQ low-stock report ဖြင့်တည်ဆောက်ပြီး duplicate, missing နဲ့negative stock tests ထည့်ပါ။
Unit testing C# in .NET — Microsoft Learn