How to define custom methods in C#?

To define a custom method in C#, you create a reusable block of code that performs a specific task. Methods help organize your code, make it more readable, and avoid repetition by allowing you to call the same functionality multiple times.

Syntax

Following is the syntax for defining a custom method in C# −

<Access Specifier> <Return Type> <Method Name>(Parameter List) {
   Method Body
}

Method Components

The following are the various elements of a method −

  • Access Specifier − This determines the visibility of a method from other classes. Common specifiers are public, private, protected, and internal.

  • Return type − A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.

  • Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.

  • Parameter list − Enclosed between parentheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.

  • Method body − This contains the set of instructions needed to complete the required activity.

Method Structure public int FindMax (int a, int b) { ... } Access Specifier Return Type Method Name Parameters Method Body Example: public int FindMax(int a, int b) { return a > b ? a : b; } This method is public, returns an integer, takes two parameters, and contains logic

Using Methods with Return Values

Example

using System;

class NumberManipulator {
   public int FindMax(int num1, int num2) {
      /* local variable declaration */
      int result;

      if (num1 > num2)
         result = num1;
      else
         result = num2;
      return result;
   }

   static void Main(string[] args) {
      /* local variable definition */
      int a = 90;
      int b = 15;
      int ret;
      NumberManipulator n = new NumberManipulator();

      //calling the FindMax method
      ret = n.FindMax(a, b);
      Console.WriteLine("Max value is : {0}", ret);
   }
}

The output of the above code is −

Max value is : 90

Using Void Methods

Methods that do not return a value use the void return type −

Example

using System;

class Calculator {
   public void DisplayResult(int result) {
      Console.WriteLine("The result is: " + result);
   }
   
   public void PerformAddition(int x, int y) {
      int sum = x + y;
      Console.WriteLine("{0} + {1} = {2}", x, y, sum);
   }

   static void Main(string[] args) {
      Calculator calc = new Calculator();
      calc.DisplayResult(150);
      calc.PerformAddition(25, 30);
   }
}

The output of the above code is −

The result is: 150
25 + 30 = 55

Methods with Multiple Parameters

Example

using System;

class MathOperations {
   public double CalculateArea(double length, double width, string shape) {
      if (shape == "rectangle") {
         return length * width;
      } else if (shape == "triangle") {
         return 0.5 * length * width;
      }
      return 0;
   }
   
   public string GetStudentGrade(string name, int score) {
      string grade = score >= 90 ? "A" : score >= 80 ? "B" : score >= 70 ? "C" : "F";
      return name + " scored " + score + " and got grade " + grade;
   }

   static void Main(string[] args) {
      MathOperations ops = new MathOperations();
      double area1 = ops.CalculateArea(10, 5, "rectangle");
      double area2 = ops.CalculateArea(8, 6, "triangle");
      
      Console.WriteLine("Rectangle area: " + area1);
      Console.WriteLine("Triangle area: " + area2);
      Console.WriteLine(ops.GetStudentGrade("Alice", 87));
   }
}

The output of the above code is −

Rectangle area: 50
Triangle area: 24
Alice scored 87 and got grade B

Conclusion

Custom methods in C# allow you to create reusable blocks of code with specific access levels, return types, and parameters. Methods improve code organization and readability by encapsulating functionality that can be called multiple times throughout your program.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements