Methods in C# Programming

A method is a block of codes that contains some statements and perform particular task. Using method increases the quality of our program a lot. Some of them are listed below:

  • It help us avoid repetition of codes.
  • Same block of code can be used several times in a program just by calling the method.
  • It reduces the chance of errors because changes only need to be made in one place
  • It will increase clarity of program because it groups related codes together which makes our program well structured and easy to understand.
  • It can also be used for information hiding.

Declaration of Method

In C# programming, a method can be declared using following syntax:

<access_specifier> <return_type> <method_name>(parameters)
{
     method_body
}

Access Specifier defines the visibility if the method or its variables in the program.

Return Type is the data type of variable returned by the method.

Method Name is the name we give for the method. It must be unique. It is considered good when method name explains the purpose of the method, and it follows pascal case rule (eg. GetName).

Parameters contains list of parameters used by a method to transfer data. It can be empty as well.

Method Body contains block of statements to perform task of the method. It is enclosed within curly brackets.

Example for Declaration of Method:

Invoking a Method (Calling a Method)

Invoking a method means calling a method and executing statements in the method body. Calling of a method can be done just by writing the method's name and followed by round brackets "( )" and semicolon ";" after that. Syntax for calling a method:

<method_name>();

Example: C# example of Method Declaration and Calling a Method

using System;

class Program
{
    static void DisplayTopic() //Declaration
    {
        Console.WriteLine("Methods in C# Programming");
    }
    static void Main()
    {
        DisplayTopic(); //Calling method
    }
}


In above program, when the program is executed, it first executes the main method which contains the metod call DisplayTopic(); which invokes the method DisplayTopic. It then goes to that method and executes statements contained by that method. The output of the above program will be:

Methods in C# Programming

A Method can be called from main Method, some other Method or the same Method from its body. This type of call is called recursion.

static void DisplayTopic()
{
    Console.WriteLine("Methods in C# Programming");
}
static void Main()
{
    DisplayTopic(); //Call from main Method
}

static void DisplayTopic()
{
    Console.WriteLine("Methods in C# Programming");
}
static void Article()
{   
    ...
    ...
    DisplayTopic(); //Call from some other Method
}

static void DisplayTopic()
{
    Console.WriteLine("Methods in C# Programming");
    DisplayTopic(); //Call from the same Method
}

Parameters in a Method

Parameters are used to pass information to a method. A method can have multiple parameters. They are specified inside round brackets "( )". 

Example: C# example showing use of parameter

Program to find greater number between two numbers.

using System;

class Program
{
    static int FindGreaterNumber(int a, int b)
    {
        if (a > b)
            return a;
        else
            return b;
    }
    static void Main()
    {
        int a, b, great;
        Console.WriteLine("Enter two numbers: ");
        a = Convert.ToInt32(Console.ReadLine());
        b = Convert.ToInt32(Console.ReadLine());
        great = FindGreaterNumber(a, b);
        Console.WriteLine("The greater number is: " + large);
    }
}

Output:

Enter two numbers:
1
2
The greater number is: 2

A Method can have variable number of arguments as well. It can be done by using params keyword.

Example: C# example showing use of variable number of arguments

Program to find sum of numbers using params.‚Äč

using System;
public class Program
{
    static void SumOfNumbers(params int[] list)
    {
        int sum = 0;
        foreach (int i in list) 
        {
            sum = sum + i;
        }
        Console.WriteLine("The sum is {0}.", sum);
    }
    
    static void Main()
    {
        SumOfNumbers(5, 10, 15);
    }
}

In above program, SumOfNumbers is called from main Method. SumOfNumbers takes parameters using params. There can be any number of numbers in the list. The number in list is accessed by using foreach keyword and the sum is calculated.

Output:

The sum is 30.