Thuta Learning
C#
AdvancedProgrammingအခြေခံ

Polymorphism

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

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

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

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

Polymorphism ကို ရိုးရိုးလေးနားလည်မယ်

Polymorphism ဆိုတာ object မတူပေမယ့် method name တူကို ခေါ်တဲ့အခါ ကိုယ်ပိုင်ပုံစံနဲ့ အလုပ်လုပ်နိုင်တာပါ။ Base class မှာ virtual ထားပြီး derived class မှာ override လုပ်နိုင်ပါတယ်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Type အလိုက်if/else အမြဲခွဲရခြင်းကmissing polymorphic contract ကိုဖော်ပြနိုင်ပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

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

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Polymorphism ကcommon abstraction ကိုကိုင်ထားသောcaller တစ်ခုကruntime implementation မျိုးစုံကိုသုံးနိုင်စေပါတယ်။ Virtual dispatch, interface dispatch, overload resolution နဲ့type tests တို့၏ကွာခြားချက်ကိုသိပါ။

Mental Model
  1. Object / dependency
  2. C# type contract + CLR
  3. Reusable behavior

Polymorphism ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

csharp
class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog says: woof");
    }
}

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat says: meow");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound();
        myCat.MakeSound();
    }
}

Polymorphism ကcommon abstraction ကိုကိုင်ထားသောcaller တစ်ခုကruntime implementation မျိုးစုံကိုသုံးနိုင်စေပါတယ်။ Virtual dispatch, interface dispatch, overload resolution နဲ့type tests တို့၏ကွာခြားချက်ကိုသိပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Object / dependencyclass Animal

    Polymorphism ဆိုတာ object မတူပေမယ့် method name တူကို ခေါ်တဲ့အခါ ကိုယ်ပိုင်ပုံစံနဲ့ အလုပ်လုပ်နိုင်တာပါ။ Base class မှာ virtual ထားပြီး derived class မှာ override လုပ်နိုင်ပါတယ်။

  2. C# type contract + CLR{

    Polymorphism ကcommon abstraction ကိုကိုင်ထားသောcaller တစ်ခုကruntime implementation မျိုးစုံကိုသုံးနိုင်စေပါတယ်။ Virtual dispatch, interface dispatch, overload resolution နဲ့type tests တို့၏ကွာခြားချက်ကိုသိပါ။

  3. Reusable behavior}

    Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။

Execution Flow
  1. Object / dependency
  2. C# type contract + CLR
  3. Reusable behavior

Success

Polymorphism ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

Failure

Type အလိုက်if/else အမြဲခွဲရခြင်းကmissing polymorphic contract ကိုဖော်ပြနိုင်ပါတယ်။

Polymorphism ဆိုတာ object မတူပေမယ့် method name တူကို ခေါ်တဲ့အခါ ကိုယ်ပိုင်ပုံစံနဲ့ အလုပ်လုပ်နိုင်တာပါ။ Base class မှာ virtual ထားပြီး derived class မှာ override လုပ်နိုင်ပါတယ်။

csharp
class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog says: woof");
    }
}

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat says: meow");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound();
        myCat.MakeSound();
    }
}

ဘာကို သတိထားရမလဲ

  • Animal myDog = new Dog(); ဆိုပြီး base type နဲ့ကိုင်ထားပေမယ့် တကယ် run တဲ့ method က Dog ထဲက override method ပါ။
  • ဒီ concept က plugin system, payment provider, notification channel လို pattern တွေမှာ အရမ်းအသုံးဝင်ပါတယ်။
You should see
The dog says: woof The cat says: meow

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
csharp
class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog says: woof");
    }
}

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat says: meow");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound();
        myCat.MakeSound();
    }
}

ရလဒ်: Polymorphism ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။

Quick Quiz 1 / 3

Polymorphism ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Object / dependency → C# type contract + CLR → Reusable behavior
  • Polymorphism ကcommon abstraction ကိုကိုင်ထားသောcaller တစ်ခုကruntime implementation မျိုးစုံကိုသုံးနိုင်စေပါတယ်။ Virtual dispatch, interface dispatch, overload resolution နဲ့type tests တို့၏ကွာခြားချက်ကိုသိပါ။
  • Type အလိုက်if/else အမြဲခွဲရခြင်းကmissing polymorphic contract ကိုဖော်ပြနိုင်ပါတယ်။
  • Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။

Object-oriented programming in C# — Microsoft Learn

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

  • Type အလိုက်if/else အမြဲခွဲရခြင်းကmissing polymorphic contract ကိုဖော်ပြနိုင်ပါတယ်။
  • Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။

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

Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: The dog says: woof The cat says: meow

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

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

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

Polymorphism | Thuta Learning