C# Methods3 min read

A method refers to a block of code (or a group of statements) that performs a specific task. It takes some inputs, does some computation based on that input and returns the output. They are also known as functions.

The use of function in a program helps in many ways such as reuse of code, Optimization of Code. The function is declared within the curly braces {}.

Every C# program has at least one function which is always executed and that is the main function from where the execution of the program begins.

In order to use a method in a program, you need to:

  • define a method
  • call a method

Defining Methods in C#

Defining a method means to write its structure with all of its components. Generally, the function is defined in following way in C#:

Followings are the list of component present in a method.

  • Access Specifier: It defines its visibility to the other class, which means it specifies the variables and methods accessibility in the program.
  • Return type: A function may return a value unless it is a void function, which does not return any type of value. return_type is a data type of the function. It could return arithmetic values (int, float, etc.), pointers, structures, etc.
  • Function name: Function name refers to identifiers through which function can be identified uniquely. It is a function’s name that is stated to function by following some naming rules.
  • Parameter list: Parameter is also referred to as Argument. We can pass the values through the parameter when the function is invoked. The parameter list refers to the type, order, and number of the parameters of a function
  • Function Body: The function body is enclosed within the curly braces’{}’. It defines the task of a method within the curly braces.

Remember that you can also leave the parameter list empty if you are not passing any value to the function.

Calling Methods in C#

To call a method means to execute the method and to do that we need to write a function name by passing the values as parameters (if any) within the two parentheses () and semicolon.

Following is the syntax t call a function.

Example: C# example of method

let us see an example in C# where we create an addition function and pass the value to add and return the result to the main function. We will see the use of defining and calling a method in C#.


Let us see the use of Access Specifier in the C# program. The program below will have two classes and the method of one class can be accessed by another class by using the instance of the class. We need to use the public Access Specifier.


How to pass Parameter in Methods?

We need to pass the values to a method in order to calculate just like an example shown above, we can do that in three different ways in C#.

  • call by value
  • call by reference
  • out parameter

We will got through each of them in next tutorial.


Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …