C# Tutorial – Part 2.1: Method

In the last tutorial, I introduced to you the IDE I used for my C# project and guided you on how to use it. Also, you learned a few essential basic syntaxes in C# programming language. I hope you also went to the link I included to learn more about C# syntax. In the #2 tutorial, I will cover 2 other concepts that support you to make an efficient program.

  1. Method
  2. Class

To make it easy to follow, I will separate the #2 tutorial into 2 parts. I will cover the knowledge about method in this part and the knowledge about class in the next part.

What is the method in C#? Why do we need it? How to use it?

A method in C# is also known as a function in other programming languages. It is a block of code that can do a specific action or even a series of actions. I would suggest making a method that can a specific action rather than a series of actions because it looks more practical (easier to debug). Let’s look at the program without using any method.

The program without using any method

The program without using any method

In this program, we have 3 variables/elements (a, b, c). In order to know whether the sum of each pair is greater than 20 or not, we need to use the if-else statement 3 times and it consumes 20 lines of code. What if we have more than 3 variables/elements? It can be up to hundreds of lines of code if we still use the if-else statement. In order to solve this problem, we come up with constructing a method that does the same comparing action with many different pairs. Let’s look at the program with the use of method.

The program with the use of method

The program with the use of method

In this program, we shorten 20 lines of code above to 10 lines of code, so it looks more efficient when we have more than 3 variables. With a comparingSum method, our job now is just calling a method and passing a pair of elements by 1 line of code. That’s why we need the method. There are a few things about the method I would like to introduce.

  • Line 20-30: This is how you construct the method in the main class which is used to execute the output of your program. The syntax for this is[static] [returned type] [method's name] [(paremeters)] { ... }.
    • [static] is the keyword/modifier that helps the program to know that there is only 1 copy of the method. This copy of the method is used in all instances/objects of the class. Since the Main method is the static method, we need to use the static keyword for the constructed method.
    • [returned type] is the type of value that returned from the method. In this case, the function returns nothing. As a result, we use void as a returned type of method. You will see the method that returns value later in this tutorial
    • [(parameters)] is a value or a series of values which the method receives. In this case, the comparingSum method receives 2 parameters/values because we need to compare the sum of a pair of elements.
  • Line 15-17: This is how you call a method and pass value(s) to it. The syntax for this is [method's name][(arguments)].
    • Since a method returns nothing, you can use this syntax to call it.
    • The argument is used when you call the method and pass/give some values to it. In my view, it looks like the parameter. The only thing you need to know is that it has different names in different scenarios.
The result of the program in 2 cases

The result of the program in 2 cases

Now, you know what is the method and why we need it. In the example above, there is, however, the method which returns nothing. What if you want the method to return a value? Look at this.

A method returns an output

A method returns a value

In this example, I constructed a method which calculates the result of the linear equation by giving 3 values (a, b, x). After calculating, it returns the integer value. In order to catch the returned value, I initialize/create a variable in the main method which has a similar data type with the return value. There are a few things you need to know here.

  • Line 15-17: This is how you catch the returned value from a method. A syntax for this is [data type] [variable's name] = [method's name][(arguments)].
  • Line 25: Instead of using void for the method which returns nothing, I use int as a returned type to specify that this method will return an integer value.
  • Line 33: With the return-value method, you need to have a return statement so that the method is able to know which value is returned. Importantly, the returned value must have a similar data type with the returned type of the method.

Now, let’s try to refactor the block of code [line 15-22] (refactoring is the way how you make your code readable and reusable). For now, the block of code in the main method is a bit short since we have only 3 variables. What if we have more than 3 variables? I will give you a solution to this problem, but it is important for you to think about it.

The code after refactoring

The code after refactoring

I don’t explain this refactoring code since I let you think about it. Two examples above give a similar output.

The output of the return-value method

The output of the return-value method

These are a few essential things you need to know about method in C# programming language. I encourage you to learn more on Google and give a try by making your own program. Moreover, you should think about the refactoring code I have given above so that you will understand why we need to refactor the code.


That’s it for the first part of the #2 tutorial. Hopefully, it helps you make more sense about method in C# programming language now. I will publish the second part of the #2 tutorial soon. If you have any question or any topics you want to learn more, please let me know.

If the images in this post are unclear, I have uploaded them to my Github repository. You can visit this link to have clearer images.

Thank you for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s