Methods in C# and also in other programming languages are used to define behavior. They are used to write some focused logic used to complete a specific task and can be invoked, or called, to execute the code written inside of them. Methods inside classes have access modifiers, with the default being private. This means that the method is only available to use inside the same class. To make the method available to an entire project, you can use the internal keyword. If you were to use the public keyword, this makes the method available anywhere. Let’s learn a bit more about methods, fields, events, and properties in C# now.

Return Types

All methods have a return type. What does this mean? When you call a method, it can return a value or not. If it does not, it still has a return type and we call that a void return type. If a method is typed as void, it simply means that the method doesn’t return a value to the calling code. Here is an example of a method with a return type of void.

static void WriteResult(string description, int result)
{
    Console.WriteLine(description + ": " + result);
}

When the code above is invoked, it does not return a value and therefore it has a type of void. Notice there is no return statement. The code listed below however does use a return statement. When the ComputeStatistics() method runs, it returns an object.

public StockStatistics ComputeStatistics()
{
    StockStatistics stats = new StockStatistics();

    float sum = 0;
    foreach (float stock in stocks)
    {
        stats.HighestStock = Math.Max(stock, stats.HighestStock);
        stats.LowestStock = Math.Min(stock, stats.LowestStock);
        sum += stock;
    }

    stats.AverageStock = sum / stocks.Count;
    return stats;
} 

Methods May Take Parameters

Methods may have one or more parameters in their method signature, or definition. Don’t get parameters and arguments mixed up however. We refer to data passed to a method as a parameter in its defining code, and as an argument when the method is actually called. Parameters in C# area typed. This means we specify the data type of the parameters that will be passed to the method. Consider this code here.
static void WriteResult(string description, float result)

{
    Console.WriteLine($"{description}: {result:F2}", description, result);
}

The code above takes two parameters. The first has a type of string, and used the name of description. So inside the method, description is going to hold a string data type. It also has a parameter of type float with a name of result. So again, inside the function any time you see result, you know it will contain a float data type.

C# Method Signatures

The signature of a method consists of a method name and the type and kind of each of its parameters. This is a unique identifier to the C# compiler. The method signature does not include a return type. Any legal character can be used in the name of a method. Method names typically begin with an uppercase letter. The names chosen should be verbs or verbs followed by adjectives or nouns. This helps identify that a method performs an action.

Methods Can Be Overloaded

Methods have the ability to be overloaded. What does that mean? It means that a method can have the same exact name of another method, and the C# compiler will be ok with that. The rule however is that each method with a same name must differ in the type or number of parameters that the method accepts. An example of an overloaded method that is a part of the C# language is the commonly used WriteLine() method. In Visual Studio, we can even see that intellisense gives us a message that Console.WriteLine() has 18 overloads.

C# Methods And Properties
1.20 GEEK