Thuta Learning
IntermediateProgrammingbeginner

Methods

Relax. We'll talk through this in plain words — no textbook voice.

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

  • SayHello method takes a name as a parameter and prints out a greeting.
  • Add method takes two numbers and returns their sum.
  • void means the method doesn't return a value.
  • int return type has to hand back an int value.
You should see
Hello, Aung! Total: 8
Methods | Thuta Learning