Thuta Learning
C#
IntermediateProgrammingbeginner

Method Overloading

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
Method Overloading | Thuta Learning