Hello and welcome to the Methods in C# article in which we are going to talk… well about methods :D. Furthermore, we are going to learn how to use methods in C#, what is the method signature, and how to use parameters and arguments.

For the complete navigation of this series check out: C# Back to Basics.

If you want to download the source code for our examples, you can do that from here Methods in C# Source Code.

In this article, we are going to talk about:

In our previous article, we have talked about access modifiers (which are an important part of every method), so we strongly suggest to read it if you haven’t already.

A method is a code block we can use to extract part of our code to reuse it, thus making our classes more readable and easier to maintain. We can execute all the code inside a method once we call that method by using its name and specifying required arguments.

Method Signatures

We can declare our methods by specifying the method signature that consists of the access modifier (public, private…), a return value (void, int, double…), a name of a method, and method parameters. If we want our method to have an implementation, it needs to have two curly braces to specify the body of the method. We place our code between those curly brackets.

A method that returns a value needs to satisfy two conditions. First, it needs to specify a return type before the method name. The second, it needs to have a return statement within its body (inside curly braces). On the other hand, if the method doesn’t return anything, the void keyword is used instead of the return keyword. If that’s the case, a method doesn’t need to have a return statement inside its body:

Method signatures - Methods in C#

In our project, we can have two different methods with the same name, but we can’t have two different methods with the same method signature. At least one part of the method signature needs to be different. When we have two or more methods with the same name but different signature, that’s called Method Overloading.

Parameters and Arguments

In the previous example, we have seen that our methods accept only one parameter. But, we can create a method that accepts as many parameters as we need:

It is important that every parameter has its own type, name and that they are comma separated.

When we create a method in the signature, we create parameters (imagine them as the placeholders for the value of the same type). But, when we call that method we are passing real values (arguments) for those parameters:

Example 1: Create an application which prints out the sum, subtraction, and multiplication of two inputs:

Methods in C# Example 1

Optional Parameters

An optional parameter has a default value. The method that has optional parameters could be called without those arguments. But we can provide them as well. If we provide the values as arguments for optional parameters then the default values will be overridden:

Conclusion

Using methods is very useful, not only in C# but in programming overall. So having this knowledge is quite an advantage. Don’t be afraid to use them while coding, they will make your code cleaner, maintainable, readable, and above all reusable.

In our next post, you will learn more about Ref and Out keywords in C#.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.