Methods
If variables store some values, then methods contain a set of instructions that perform specific actions. Essentially, a method is a named block of code that performs certain actions.
The general definition of methods looks as follows:
[modifiers] return_type method_name([parameters])
{
// method body
}
Modifiers and parameters are optional.
Earlier, we have already used at least one method — Console.WriteLine(), which outputs information to the console. Now let’s look at how we can create our own methods.
Method Definition
Let’s define one method:
void SayHello()
{
Console.WriteLine("Hello");
}
Here, the method SayHello is defined, which outputs a certain message. Method names follow basically the same rules as variable names. However, as a rule, method names start with a capital letter.
Before the method name comes the return data type. Here, it is the void type, which indicates that it doesn’t actually return anything, it simply performs some actions.
After the method name in parentheses comes a list of parameters. But in this case, the parentheses are empty, which means that the method does not take any parameters.
After the list of parameters in parentheses comes a block of code that represents the set of instructions performed by the method. In this case, the block of the SayHello method contains only one instruction that outputs a string to the console:
Console.WriteLine("Hello");
But if we run this project, we won’t see any string that the SayHello method is supposed to print. That’s because after defining the method, we also need to call it to make it perform its job.
Calling Methods
To use the SayHello method, we need to call it. To call a method, specify its name, followed by values for its parameters in parentheses (if the method accepts parameters).
method_name(values_for_method_parameters);
For example, the call to the SayHello method would look like this:
SayHello();
Since the method doesn’t accept any parameters, empty parentheses follow the method name.
Let’s combine the method definition and the method call:
void SayHello()
{
Console.WriteLine("Hello");
}
SayHello(); // Hello
SayHello(); // Hello
Console output of the program:
Hello Hello
The advantage of methods is that they can be called repeatedly and in different parts of the program. For example, in the example above, the SayHello method is called twice.
At the same time, in this case, it doesn’t matter whether the method is defined first and then called, or vice versa. For example, we could also write it like this:
SayHello(); // Hello
SayHello(); // Hello
void SayHello()
{
Console.WriteLine("Hello");
}
Let’s define and call a few more methods:
void SayHelloRu()
{
Console.WriteLine("Привет");
}
void SayHelloEn()
{
Console.WriteLine("Hello");
}
void SayHelloFr()
{
Console.WriteLine("Salut");
}
string language = "en";
switch (language)
{
case "en":
SayHelloEn();
break;
case "ru":
SayHelloRu();
break;
case "fr":
SayHelloFr();
break;
}
Here, three methods are defined: SayHelloRu(), SayHelloEn(), and SayHelloFr(), which also have the void type, do not accept any parameters, and also print a certain string to the console. Figuratively speaking, they print greetings in a specific language.
In the switch construct, the value of the language variable is checked, which conditionally stores the language code, and depending on its value, a specific method is called. So, in this case, the console will display:
Hello
Shortened Method Syntax
If a method defines only one instruction in its body, then we can shorten the method definition. For example, suppose we have a method:
void SayHello()
{
Console.WriteLine("Hello");
}
We can shorten it like this:
void SayHello() => Console.WriteLine("Hello");
That is, after the parameter list, we put the => operator, followed by the instruction to be executed.