Skip to main content

Methods

While variables store some values, methods contain a set of instructions that perform certain actions. In essence, a method is a named block of code that performs some actions.

The general definition of methods is as follows:

[modifiers] type_return_value name_method ([parameters])
{
// method body
}

Modifiers and parameters are optional.

We have previously 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.

Defining a method

Let's define one method:

void SayHello()
{
Console.WriteLine(“Hello”);
}

The SayHello method is defined here, which outputs some message. Method names have basically the same requirements as variable names. However, as a rule, method names begin with a capital letter.

The method name is preceded by the returned data type. Here it is the void type, which indicates that it does not actually return anything, it just performs some actions.

After the method name, the parameters are listed in brackets. But in this case the parentheses are empty, which means that the method does not accept any parameters.

After the list of parameters in parentheses comes a code block, which represents the set of instructions executed by the method. In this case, the SayHello method block 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 should output. That's because once the method is defined, it still needs to be called to do its job.

Calling methods

To use the SayHello method, we need to call it. To call a method, we specify its name, followed by the values for its parameters (if the method accepts parameters) in parentheses.

method_name (values_for_the_parameters_of_the_method);

For example, a call to the SayHello method would look like this:

SayHello();

Since the method does not accept any parameters, the method name is followed by empty brackets.

Let's combine the 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 repeatedly in different parts of the program. For example, in the example above, the SayHello method is called twice.

In this case, it makes no difference whether the method is defined first and then called or vice versa. For example, we could write like this:

SayHello(); // Hello
SayHello(); // Hello

void SayHello()
{
Console.WriteLine(“Hello”);
}

Let's define and call a few more methods:

void SayHelloRu()
{
Console.WriteLine(“Hello”);
}
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;
}

There are three methods defined here SayHelloRu(), SayHelloEn() and SayHelloFr(), which are also of type void, take no parameters and also output some string to the console. Conventionally speaking, they output a greeting in a particular language.

In the switch construct, the value of the language variable, which conditionally stores the language code, is checked, and depending on its value, a certain method is called. So, in this case, the console will display

Hello

Abbreviated method notation

If a method defines only one instruction as its body, we can shorten the method definition. For example, let's say we have a method:

void SayHello()
{
Console.WriteLine(“Hello”);
}

We can shorten it as follows:

void SayHello() => Console.WriteLine(“Hello”);

That is, the => operator is placed after the list of parameters, followed by the instruction to be executed.