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 the required arguments.

Method Signatures

We can declare our methods by specifying the method signature that consists of the access modifier (public, private…), the 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. Additionally, we have to include a return value (void, int, double…) for our method to be valid. The return type doesn’t apply as a part of the method signature, but we can’t create a method without it. Thus it injects itself into a signature (or specification).

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:

public void WriteAllNumbers(int a, int b, int c)
{
     Console.WriteLine($"{a} {b} {c}");
}

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:

WriteAllNumbers(15, 16, 67);

Example 1: Create an application which prints out the sum, subtraction, and multiplication of two inputs:
class Program
{
    public static void Sum(int first, int second) //method needs to be static because we are calling it in a static Main method.
    {
        int result = first + second;
        Console.WriteLine($"Sum result: {result}");
    }

    public static void Subtract(int first, int second)
    {
        int result = first - second;
        Console.WriteLine($"Substraction result: {result}");
    }

    public static void Multiplication(int first, int second)
    {
        int result = first * second;
        Console.WriteLine($"Multiplication result: {result}");
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Enter the first number: ");
        int firstArgument = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter the second number: ");
        int secondArgument = Convert.ToInt32(Console.ReadLine());

        Sum(firstArgument, secondArgument);
        Subtract(firstArgument, secondArgument);
        Multiplication(firstArgument, secondArgument);

        Console.ReadKey();
    }
}

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:

public static void MethodWithOptParams(int first, int second = 10)
{
    Console.WriteLine(first + second);
}

MethodWithOptParams(20); //result is 30
MethodWithOptParams(20, 35); //result is 55

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#.