What are methods in C#?

Methods are also known as the functions of a class. They are primarily used to provide code reusability, which saves computational time and makes the code well structured and clean. Methods also increase code readability since the same set of operations do not need to be written again and again.

How are methods used?

Methods are declared in the class and called in the main program.

When declaring a method, its return type, name, and parameters are defined and the actions to be performed are placed within curly braces { }. For example, a multiplication function can be declared as:

int multiply(int a, int b)
{
    return a * b;
}
  • int before multiply is the return type of the method.
  • multiply is the name of the method.
  • (int a, int b) are the parameters of this method. These values can be used inside the method.

Code

The following code uses the multiply method and displays the multiplied result of the two numbers.

using System;
class methods{
int multiply(int a, int b)
{
int res = a * b;
return res;
}
public static void Main(string[] args)
{
int a = 10;
int b = 3;
methods object1= new methods();
int result;
Console.WriteLine("Result of the multiplication is ");
result=object1.multiply(a, b);
Console.WriteLine(result);
}
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved