Methods

A method allows you to encapsulate a piece of code and call it from other parts of your code. You may very soon run into a situation where you need to repeat a piece of code, from multiple places, and this is where methods come in. In C#, they are basically declared like this:

<visibility> <return type> <name>(<parameters>)
{
	<method code>
}

To call a method, you simply write its name, an open parenthesis, then parameters, if any, and then a closing parenthesis, like this:

DoStuff();

Here is an example of our DoStuff() method:

public void DoStuff()
{
    Console.WriteLine("I'm doing something...");
}

The first part, public, is the visibility, and is optional. If you don't define any, then the method will be private. More about that later on. 
Next is the type to return. It could be any valid type in C#, or as we have done it here, void. A void means that this method returns absolutely nothing. Also, this method takes no parameters, as you can see from the empty set of parentheses. Let's change that:

public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    return result;
}

We've changed almost everything. The method now returns an integer, it takes two parameters (both integers), and instead of outputting something, it makes a calculation and then returns the result. This means that we can add two numbers (a mathematical PROCESS) from various places in our code, simply by calling this method, instead of having to write the calculation code each time. While we don't save that much time and effort in this small example, you better believe that you will learn to lovemethods, the more you use C#. This method is called like this:

int result = AddNumbers(10, 5);
Console.WriteLine(result);

As mentioned, this method actually returns something, and it has to, because we told C# that it's supposed to do so. When declaring anything else than void as a return type, we are forcing our method to return something. You can try removing the return line from the example above, and see the compiler complain: 

'AddNumbers(int, int)': not all code paths return a value 

The compiler is reminding us that we have a method which doesn't return ny value, although we promised it would. And the compiler is pretty clever! Instead of removing the line, try something like this:

public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    if(result > 10)
    {
        return result;
    }
}

You will see the exact same error - but why? Because there is no guarantee that our if condition will evaluate to true and so there may not be any value to the return. You can solve this by having a second, default like return statement in the end:

public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    if(result > 10)
    {
        return result;
    }
    return 0;
}

This will fix the problem we created for our method, and it will also show you that we can have more than one return statement in our method. As soon as a return statement is reached, the method is left and no more code in it is executed. In this case, it means that as long as the result is higher than 10, the "return 0" is never reached.

Programming 2015 -2016