Methods in C#

Packing a set of statements into a single module helps in reducing writing of same code again and again or using any specific operation again and again. Writing multiple statements under a common name (method name) and calling that name numerous times helps save programming efforts to write the same code gives the ability to reuse the same code to save excessive memory use. In this chapter, you will learn about the use of methods in C# and their syntax and different characteristics.

What Are Methods in C#?

Methods are a group of statements that can be put together to perform a specific operation and return the result to the calling method section and also helps reduce writing the same code multiple times. It also helps in code reusability. Every C# program has at least one method that is the popular Main(). There are two significant parts of a method you need to place in a program to execute them successfully. These are:

  • The method definition
  • The method Call
<Access_Modifier> <return_type> <method_name> ([<param_list>])
// Body of the method
public int min (int a, int b)
    if(a < b)
        return a;
        return b;

Different Parts of a Method:

  • Access Modifier: These are used for defining access type of any method, i.e., how the accessing of data will take place. C# provides three different access modifiers: Public, Protected, Private (which will be covered in details in the later chapters).
  • Return type: It determines what kind of data will be returned by this method. When the return type is void, it means the method is returning nothing.
  • Method Name: This section of syntax specifies the name of the method (user defined), using which the programmer will call the entire method.
  • The body of the Method: This determines the line of code (enclosed within curly braces) that will perform the specific operation or task for which the method was designed.
  • Parameter list:¬†These are valid identifiers separated by the delimiter comma used for inputting parameters of different types, and a data type precedes each of them. They are enclosed within parenthesis. It is also possible that the method can have no parameter in the parameter list.

Method Signature

Two significant constraints define the method signature. These are:

  1. Method Name.
  2. Method Parameter.

Here parameter name consists of parameter numbers, type and order of parameters. This concept is used in method overloading which will be covered in the later chapters.

Method Invocation or Method Calling

This process is performed when the programmer wants to execute any method, and so it needs to be called using the functionality. As soon as the method name invokes the method, it goes to the method definition where all the statements inside it get executed. Then the return statement is executed, and the final value is returned to the calling method.

using System; 
namespace ApplicationOne {      
class calc { 
    static int Summatn(int a, int b) // called method
    {     // method definition
            int g = a; 
            int h = b; 
            int res = g + h; 
            return res; 
    static void Main(string[] args) 
        int g = 62; 
        int h = 24; 
        int i = Summatn(g, h); // calling method
        Console.WriteLine(" Your Sum value is :" + i); 
Your Sum value is :86

Scroll Back to Top