Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Writing your first method

A method is a named block of work. You write it once and call it wherever you need it, which removes repetition from a program.

The parts of a method

C#
private int AddTax(int price, int rate)
{
    int tax = price * rate / 100;
    return price + tax;
}

Reading the signature

  • The return type says what the method gives back.
  • The name says what it does, in a verb.
  • The parameters say what it needs from the caller.
Name it for the caller. A good method name lets another developer use it without reading the body. If you cannot name it briefly, it probably does more than one thing.

Methods live inside a class, so the natural next step is defining a class of your own.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.