DEV Community

Cover image for C# Methods
Zafar Urakov
Zafar Urakov

Posted on

C# Methods

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

The general definition of methods is as follows:

[modifiers] return_value_type method_name ([parameters])
{
    // method body
}
Enter fullscreen mode Exit fullscreen mode

Modifiers and parameters are optional.

We have previously used at least one method - Console.WriteLine(), which prints 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");
}
Enter fullscreen mode Exit fullscreen mode

The SayHello method is defined here, which prints some message. In principle, the same requirements apply to method names as to variable names. However, as a rule, method names begin with a capital letter.

The method name is preceded by the return data type. Here it's a void type, which indicates that it doesn't actually return anything, it just does some things.

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

Following the list of parameters in parentheses is a block of code that represents the set of instructions the method executes. In this case, the SayHello method block contains only one instruction, which prints the line to the console:

Console.WriteLine("Hello");
Enter fullscreen mode Exit fullscreen mode

But if we run this project, we will not see any line that the SayHello method should output. Because after definition, the method still needs to be called in order for it to do its job.

Calling Methods

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

method_name (values_for_method_parameters);
Enter fullscreen mode Exit fullscreen mode

For example, calling the SayHello method would look like this:

SayHello();
Enter fullscreen mode Exit fullscreen mode

Since the method does not take any parameters, the method name is followed by empty parentheses.

Let's combine the definition and method call:

void SayHello()
{
    Console.WriteLine("Hello");
}

SayHello(); // Hello
SayHello(); // Hello
Enter fullscreen mode Exit fullscreen mode

Console output of the program:

Hello
Hello
Enter fullscreen mode Exit fullscreen mode

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, there is no difference whether the method is first defined and then called, or vice versa. For example, we could write like this:

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

void SayHello()
{
    Console.WriteLine("Hello");
}
Enter fullscreen mode Exit fullscreen mode

Let's define and call a few more methods:

void SayHelloRu()
{
    Console.WriteLine("Привет");
}
void SayHelloEn()
{
    Console.WriteLine("Hello");
}
void SayHelloUz()
{
    Console.WriteLine("Assalomu Aleykum");
}


string language = "uz";

switch (language)
{
    case "en": 
        SayHelloEn();
        break;
    case "ru":
        SayHelloRu();
        break;
    case "uz":
        SayHelloUz();
        break;
}
Enter fullscreen mode Exit fullscreen mode

Three methods are defined here: SayHelloRu(), SayHelloEn() and SayHelloUz(), which are also of type void, do not take any parameters and also output some string to the console. Relatively speaking, they display a greeting in a specific language.

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

Assalomu Aleykum
Enter fullscreen mode Exit fullscreen mode

Shorthand for methods

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

void SayHello()
{
    Console.WriteLine("Hello");
}
Enter fullscreen mode Exit fullscreen mode

We can shorten it like this:

void SayHello() => Console.WriteLine("Hello");
Enter fullscreen mode Exit fullscreen mode

This is called "Syntactic Sugar", that is, after the list of parameters there is an => operator, followed by the instruction to be executed.

I have a lot of interesting code on GitHub

Top comments (0)