Thuta Learning
C#
IntermediateProgrammingအခြေခံ

Method Overloading

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

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

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

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

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

Method Overloading ဆိုတာ method name တူပေမယ့် parameter အရေအတွက် သို့မဟုတ် parameter type မတူတဲ့ methods များကို class တစ်ခုထဲမှာ ရေးနိုင်တာပါ။ အသုံးပြုသူဘက်က method name တစ်ခုတည်းကိုမှတ်ရုံနဲ့ input မတူတာတွေကို ကိုင်တွယ်နိုင်ပါတယ်။

Problem → Why → Solution

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

Semantics မတူသောoperations ကိုname တစ်ခုထဲoverload အများကြီးလုပ်ခြင်းကAPI ကိုခန့်မှန်းရခက်စေပါတယ်။

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

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

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

Overload resolution ကိုcompile time တွင်method name နဲ့parameter signatures အပေါ်ဆုံးဖြတ်ပါတယ်။ Return type တစ်ခုတည်းကoverload မဖြစ်နိုင်ဘဲ implicit conversions, optional parameters နဲ့`params` ကambiguity ဖြစ်စေနိုင်ပါတယ်။

Mental Model
  1. Typed input
  2. C# expression + method
  3. CLR value / object state

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

အဓိက Syntax / Pattern

csharp
class Program
{
    static int Add(int x, int y)
    {
        return x + y;
    }

    static double Add(double x, double y)
    {
        return x + y;
    }

    static int Add(int x, int y, int z)
    {
        return x + y + z;
    }

    static void Main(string[] args)
    {
        Console.WriteLine(Add(8, 5));
        Console.WriteLine(Add(4.3, 6.2));
        Console.WriteLine(Add(1, 2, 3));
    }
}

Overload resolution ကိုcompile time တွင်method name နဲ့parameter signatures အပေါ်ဆုံးဖြတ်ပါတယ်။ Return type တစ်ခုတည်းကoverload မဖြစ်နိုင်ဘဲ implicit conversions, optional parameters နဲ့`params` ကambiguity ဖြစ်စေနိုင်ပါတယ်။

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

  1. Typed inputclass Program

    Method Overloading ဆိုတာ method name တူပေမယ့် parameter အရေအတွက် သို့မဟုတ် parameter type မတူတဲ့ methods များကို class တစ်ခုထဲမှာ ရေးနိုင်တာပါ။ အသုံးပြုသူဘက်က method name တစ်ခုတည်းကိုမှတ်ရုံနဲ့ input မတူတာတွေကို ကိုင်တွယ်နိုင်ပါတယ်။

  2. C# expression + methodstatic int Add(int x, int y, int z)

    Overload resolution ကိုcompile time တွင်method name နဲ့parameter signatures အပေါ်ဆုံးဖြတ်ပါတယ်။ Return type တစ်ခုတည်းကoverload မဖြစ်နိုင်ဘဲ implicit conversions, optional parameters နဲ့`params` ကambiguity ဖြစ်စေနိုင်ပါတယ်။

  3. CLR value / object state}

    Area calculation overload နှစ်ခုရေးပြီး int/double arguments, named arguments နဲ့ambiguous candidate များကိုစမ်းပါ။

Execution Flow
  1. Typed input
  2. C# expression + method
  3. CLR value / object state

Success

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

Failure

Semantics မတူသောoperations ကိုname တစ်ခုထဲoverload အများကြီးလုပ်ခြင်းကAPI ကိုခန့်မှန်းရခက်စေပါတယ်။

Method Overloading ဆိုတာ method name တူပေမယ့် parameter အရေအတွက် သို့မဟုတ် parameter type မတူတဲ့ methods များကို class တစ်ခုထဲမှာ ရေးနိုင်တာပါ။ အသုံးပြုသူဘက်က method name တစ်ခုတည်းကိုမှတ်ရုံနဲ့ input မတူတာတွေကို ကိုင်တွယ်နိုင်ပါတယ်။

csharp
class Program
{
    static int Add(int x, int y)
    {
        return x + y;
    }

    static double Add(double x, double y)
    {
        return x + y;
    }

    static int Add(int x, int y, int z)
    {
        return x + y + z;
    }

    static void Main(string[] args)
    {
        Console.WriteLine(Add(8, 5));
        Console.WriteLine(Add(4.3, 6.2));
        Console.WriteLine(Add(1, 2, 3));
    }
}

Compiler ဘယ်လိုရွေးလဲ

Method ကိုခေါ်တဲ့အခါပေးလိုက်တဲ့ argument type နဲ့ အရေအတွက်ကိုကြည့်ပြီး သင့်တော်တဲ့ method ကို compiler ကရွေးပါတယ်။ Add(8, 5) က int version ကိုသွားပြီး Add(4.3, 6.2) က double version ကိုသွားပါတယ်။

You should see
13 10.5 6

Real-world usage

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

Area calculation overload နှစ်ခုရေးပြီး int/double arguments, named arguments နဲ့ambiguous candidate များကိုစမ်းပါ။

Mini challenge

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

Area calculation overload နှစ်ခုရေးပြီး int/double arguments, named arguments နဲ့ambiguous candidate များကိုစမ်းပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
csharp
class Program
{
    static int Add(int x, int y)
    {
        return x + y;
    }

    static double Add(double x, double y)
    {
        return x + y;
    }

    static int Add(int x, int y, int z)
    {
        return x + y + z;
    }

    static void Main(string[] args)
    {
        Console.WriteLine(Add(8, 5));
        Console.WriteLine(Add(4.3, 6.2));
        Console.WriteLine(Add(1, 2, 3));
    }
}

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

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

Area calculation overload နှစ်ခုရေးပြီး int/double arguments, named arguments နဲ့ambiguous candidate များကိုစမ်းပါ။

Quick Quiz 1 / 3

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

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Typed input → C# expression + method → CLR value / object state
  • Overload resolution ကိုcompile time တွင်method name နဲ့parameter signatures အပေါ်ဆုံးဖြတ်ပါတယ်။ Return type တစ်ခုတည်းကoverload မဖြစ်နိုင်ဘဲ implicit conversions, optional parameters နဲ့`params` ကambiguity ဖြစ်စေနိုင်ပါတယ်။
  • Semantics မတူသောoperations ကိုname တစ်ခုထဲoverload အများကြီးလုပ်ခြင်းကAPI ကိုခန့်မှန်းရခက်စေပါတယ်။
  • Area calculation overload နှစ်ခုရေးပြီး int/double arguments, named arguments နဲ့ambiguous candidate များကိုစမ်းပါ။

C# documentation — Microsoft Learn

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

  • Semantics မတူသောoperations ကိုname တစ်ခုထဲoverload အများကြီးလုပ်ခြင်းကAPI ကိုခန့်မှန်းရခက်စေပါတယ်။
  • Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။

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

Area calculation overload နှစ်ခုရေးပြီး int/double arguments, named arguments နဲ့ambiguous candidate များကိုစမ်းပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: 13 10.5 6

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

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

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

Method Overloading | Thuta Learning