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.


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 …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More