ရိုးရိုးလေး ရှင်းပြမယ်
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 ဖြစ်စေနိုင်ပါတယ်။
- Typed input
- C# expression + method
- CLR value / object state
Method Overloading ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
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 ကို အဆင့်လိုက်ဖတ်မယ်
- Typed input
class ProgramMethod Overloading ဆိုတာ method name တူပေမယ့် parameter အရေအတွက် သို့မဟုတ် parameter type မတူတဲ့ methods များကို class တစ်ခုထဲမှာ ရေးနိုင်တာပါ။ အသုံးပြုသူဘက်က method name တစ်ခုတည်းကိုမှတ်ရုံနဲ့ input မတူတာတွေကို ကိုင်တွယ်နိုင်ပါတယ်။
- C# expression + method
static 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 ဖြစ်စေနိုင်ပါတယ်။
- CLR value / object state
}Area calculation overload နှစ်ခုရေးပြီး int/double arguments, named arguments နဲ့ambiguous candidate များကိုစမ်းပါ။
- Typed input
- C# expression + method
- 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 မတူတာတွေကို ကိုင်တွယ်နိုင်ပါတယ်။
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 ကိုသွားပါတယ်။
13 10.5 6Real-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 ကိုခန့်မှန်းပါ
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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- 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