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#.


Example:

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.

Output:


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.


MORE

C Program to Convert Decimal to Binary

In this tutorial, we will write a C program to convert decimal to binary. Before that, you must have knowledge of the following topics in …
Read More

C Program to Convert Binary to Decimal

In this tutorial, we will write a program to convert binary to decimal in C. Before that, you must have knowledge of the following topics …
Read More

C Program to Print ASCII Value of a Character

In this tutorial, we will write a program to print ASCII value of a character in C programming. Before that, you should have knowledge on …
Read More

Java Program to Convert Binary to Hexadecimal

In this tutorial, we will write a Java Program to Convert a Binary Number to Hexadecimal Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Binary

In this tutorial, we will write a Java Program to Convert a Hexadecimal Number to Binary Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Decimal

This is a Java Program to Convert a Hexadecimal Number to its decimal Equivalent. Before that, you must have knowledge of the following topics in …
Read More