ရိုးရိုးလေး ရှင်းပြမယ်
Methods ကို ရိုးရိုးလေးနားလည်မယ်
Method ဆိုတာ ပြန်သုံးလို့ရတဲ့ code block တစ်ခုပါ။ Program ကြီးလာတဲ့အခါ code တွေအားလုံးကို Main ထဲမှာပဲထည့်ထားရင် spaghetti ဖြစ်လွယ်ပါတယ်။ Method ခွဲရေးခြင်းက code ကို ဖတ်လွယ်၊ စမ်းသပ်လွယ်၊ ပြင်လွယ်စေပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Method တစ်ခုမှာ input, UI, business logic နဲ့storage ကိုပေါင်းထားခြင်းကtest နဲ့reuse ခက်စေပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Method contract တွင် name, parameters, return type, preconditions, side effects နဲ့failure behavior ပါပါတယ်။ Single responsibility, clear parameter names, return values နဲ့pure logic ကိုဦးစားပေးပြီး `ref`/`out`/optional parameters ကိုလိုမှသုံးပါ။
- Typed input
- C# expression + method
- CLR value / object state
Methods ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
class Program
{
static void SayHello(string name)
{
Console.WriteLine($"Hello, {name}!");
}
static int Add(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
SayHello("Aung");
int total = Add(5, 3);
Console.WriteLine($"Total: {total}");
}
}Method contract တွင် name, parameters, return type, preconditions, side effects နဲ့failure behavior ပါပါတယ်။ Single responsibility, clear parameter names, return values နဲ့pure logic ကိုဦးစားပေးပြီး `ref`/`out`/optional parameters ကိုလိုမှသုံးပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Typed input
class ProgramMethod ဆိုတာ ပြန်သုံးလို့ရတဲ့ code block တစ်ခုပါ။ Program ကြီးလာတဲ့အခါ code တွေအားလုံးကို Main ထဲမှာပဲထည့်ထားရင် spaghetti ဖြစ်လွယ်ပါတယ်။ Method ခွဲရေးခြင်းက code ကို ဖတ်လွယ်၊ စမ်းသပ်လွယ်၊ ပြင်လွယ်စေပါတယ်။
- C# expression + method
return x + y;Method contract တွင် name, parameters, return type, preconditions, side effects နဲ့failure behavior ပါပါတယ်။ Single responsibility, clear parameter names, return values နဲ့pure logic ကိုဦးစားပေးပြီး `ref`/`out`/optional parameters ကိုလိုမှသုံးပါ။
- CLR value / object state
}Price/quantity ကိုvalidate လုပ်ပြီး total ပြန်ပေးသောmethod ရေးကာ normal, zero, negative နဲ့overflow-adjacent inputs စမ်းပါ။
- Typed input
- C# expression + method
- CLR value / object state
Success
Methods ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Method တစ်ခုမှာ input, UI, business logic နဲ့storage ကိုပေါင်းထားခြင်းကtest နဲ့reuse ခက်စေပါတယ်။
Method ဆိုတာ ပြန်သုံးလို့ရတဲ့ code block တစ်ခုပါ။ Program ကြီးလာတဲ့အခါ code တွေအားလုံးကို Main ထဲမှာပဲထည့်ထားရင် spaghetti ဖြစ်လွယ်ပါတယ်။ Method ခွဲရေးခြင်းက code ကို ဖတ်လွယ်၊ စမ်းသပ်လွယ်၊ ပြင်လွယ်စေပါတယ်။
class Program
{
static void SayHello(string name)
{
Console.WriteLine($"Hello, {name}!");
}
static int Add(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
SayHello("Aung");
int total = Add(5, 3);
Console.WriteLine($"Total: {total}");
}
}ဒီ code က ဘာလုပ်တာလဲ
SayHellomethod က parameter အနေနဲ့ name လက်ခံပြီး greeting ထုတ်ပါတယ်။Addmethod က number နှစ်ခုလက်ခံပြီး sum ကိုreturnပြန်ပေးပါတယ်။voidဆိုတာ return value မရှိဘူးလို့ဆိုလိုပါတယ်။intreturn type ရှိတဲ့ method ကintvalue ပြန်ပေးရပါတယ်။
Hello, Aung! Total: 8Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Price/quantity ကိုvalidate လုပ်ပြီး total ပြန်ပေးသောmethod ရေးကာ normal, zero, negative နဲ့overflow-adjacent inputs စမ်းပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Price/quantity ကိုvalidate လုပ်ပြီး total ပြန်ပေးသောmethod ရေးကာ normal, zero, negative နဲ့overflow-adjacent inputs စမ်းပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
class Program
{
static void SayHello(string name)
{
Console.WriteLine($"Hello, {name}!");
}
static int Add(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
SayHello("Aung");
int total = Add(5, 3);
Console.WriteLine($"Total: {total}");
}
}ရလဒ်: Methods ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Price/quantity ကိုvalidate လုပ်ပြီး total ပြန်ပေးသောmethod ရေးကာ normal, zero, negative နဲ့overflow-adjacent inputs စမ်းပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Typed input → C# expression + method → CLR value / object state
- Method contract တွင် name, parameters, return type, preconditions, side effects နဲ့failure behavior ပါပါတယ်။ Single responsibility, clear parameter names, return values နဲ့pure logic ကိုဦးစားပေးပြီး `ref`/`out`/optional parameters ကိုလိုမှသုံးပါ။
- Method တစ်ခုမှာ input, UI, business logic နဲ့storage ကိုပေါင်းထားခြင်းကtest နဲ့reuse ခက်စေပါတယ်။
- Price/quantity ကိုvalidate လုပ်ပြီး total ပြန်ပေးသောmethod ရေးကာ normal, zero, negative နဲ့overflow-adjacent inputs စမ်းပါ။
C# documentation — Microsoft Learn