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.


Implement Insertion sort Program in C

In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in …
Read More

C Program for implementation of Selection sort

In this tutorial, we will learn and write a C program to implement selection sort using function. Before that you may go through the following …
Read More

C Program to Delete a Particular element from an Array

In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have …
Read More

C Program to sort a String in Ascending order

In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following …
Read More

C Program to find Maximum occurring Character in a String

In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through …
Read More

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More