C# Methods
Tags: webdev, beginners, programming, tutorial
Published: 2023-09-25 03:32:25

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:

```cs
[modifiers] return_value_type method_name ([parameters])
{
    // method body
}
```
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:

```cs
void SayHello()
{
    Console.WriteLine("Hello");
}
```
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:

```cs
Console.WriteLine("Hello");
```
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).

```cs
method_name (values_for_method_parameters);
```
For example, calling the SayHello method would look like this:

```cs
SayHello();
```
Since the method does not take any parameters, the method name is followed by empty parentheses.

Let's combine the definition and method call:

```cs
void SayHello()
{
    Console.WriteLine("Hello");
}
 
SayHello(); // Hello
SayHello(); // Hello
```
Console output of the program:

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

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

Let's define and call a few more methods:
```cs 
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;
}
```
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:

```cs
Assalomu Aleykum
```

**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:

```cs
void SayHello()
{
    Console.WriteLine("Hello");
}
```
We can shorten it like this:

```cs
void SayHello() => Console.WriteLine("Hello");
```
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](https://github.com/ZafarUrakov)**