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.