C# Methods Tutorial For Beginners With Examples

A method is a code block that performs a task and gets executed when it is called from the same or another program.

Method Signatures

The syntax of a C# method is shown below:
<access modifier> <return type> <method_name> (parameter1,parameter2...parameterN)
{
    return parameter1;
}              
  • Access modifier: C# methods have an access modifier which specifies how a method can be accessed. More about access modifiers will be covered in the later part of this tutorial.
  • Return type: Methods can specify a return data type. The return type is a data type compatible with the type returned by the method. If a method is not returning anything then return type should be void.
  • Method name: Every method in C# have a name which identifies the method.
  • Parameters: Parameters are the values passed to the method from outside. Methods may or may not have parameters.
  • Return values: A method may or may not return a value. If it is returning some value then return keyword should be used to do so.

Naming Convention

In C# it is a standard to follow Pascal casing for method names. As per this the method name should start with upper case and then lower case.
If the method name is a combination of two words then the second word should also start with upper case.

Methods with a return type

When a method is returning some value then the return type should be compatible with the data that is being returned.
    private int SumTheValues(int firstNumber, int secondNumber)
    {
        return firstNumber + secondNumber;
    }
    
The above method has two parameters, firstNumber and secondNumber. The method adds these two parameters and returns the result.
Below it is shown how to call this method from a C# program.
     int result = SumTheValues(245, 396);
            
In the above example the method called SumTheValues is called from a program and the returned value from the method is stored in a variable called as result.

Methods without a return type

Sometimes it may not be necessary for a method to return a value. In this case a void keyword is used instead of a data type.
    private void SumTheValues(int firstNumber, int secondNumber)
    {
        int ThirdNumber = firstNumber + secondNumber;
    }
        
Below line of code will call the SumTheValues method and pass the values 245 and 396.
     SumTheValues(245, 396);   
        

Passing Parameters to methods

Parameters can be passed to a method in two ways:
  • By Value
  • By Reference

By Value:

When some value is passed to a method then a copy is created and passed to the method. Changing this value inside a method will have no effect on the original value passed by the calling program.
    static void Main(string[] args)
    {
        int passByValue = 10;

        ChangeValue(passByValue);

        Console.WriteLine(passByValue);
        Console.ReadLine();
    }

    private static void ChangeValue(int passByValue)
    {
        passByValue = 20;
    }
            
Output of the above program will be 10.

By Reference:

Values can be passed by reference by using the ref keyword. In this case when the values of the parameter is changed inside the method then the change is reflected in the calling program.
    static void Main(string[] args)
    {
        int passByValue = 10;

        ChangeValue(ref passByValue);

        Console.WriteLine(passByValue);
        Console.ReadLine();
    }

    private static void ChangeValue(ref int passByValue)
    {
        passByValue = 20;
    }
            
Output of the above program will be 20.
A method may or may not have parameters. A method may not depend on parameters to perform a task.