Method is a reusable block of code. As a program grows, cramming everything into Main turns it into spaghetti fast. Breaking things into methods keeps your code easier to read, test, and change.
csharp
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}");
}
}What this code does
SayHellomethod takes a name as a parameter and prints out a greeting.Addmethod takes two numbers andreturns their sum.voidmeans the method doesn't return a value.intreturn type has to hand back anintvalue.
You should see
Hello, Aung! Total: 8