C# Methods
A method is a block of code that performs a specific task. It can be executed when called, and it may take inputs, process them, and return a result. Methods can be defined within classes and are used to break down complex programs into simpler, modular pieces. Methods improve code organization, readability, and maintainability by promoting code reuse.
Note: We follow the sequence of Declaration, then Class Initialization, and finally Method Calling.
Declaring a Method
The method signature consists of the method name and its parameter list. It uniquely identifies the method within its class. Below image is how we define a signature of the method.

- Access Modifier: Defines the visibility of the method.
- Return Type: Specifies the type of value the method returns.
- Method Name: The name used to call the method.
- Parameters: A list of inputs the method can take and it is optional
- Method Body: The block of code that defines what the method does
Method Calling
Method Calling is the process of invoking a method to execute its code. When a method is called, control is transferred to the method, and it performs its task before returning control back to the calling code.
1. Direct Method Calling
Direct method calling occurs when you invoke a method using an instance of its class. This is the most common way to call instance methods.
Example:
using System;
class Geeks
{
// Instance method to display a message
public void DisplayMessage()
{
Console.WriteLine("Hello from the Display "
+"Message method!");
}
static void Main(string[] args)
{
Geeks geek = new Geeks();
// Create an instance of Geeks
// Call the instance method
// directly
geek.DisplayMessage();
}
}
Output
Hello from the DisplayMessage method!
2. Static Method Calling
Static methods belong to the class itself rather than any specific instance. They can be called without creating an object of the class.
Example:
using System;
class Geeks
{
// Static method to calculate
// square of a number
public static int Square(int number)
{
// Return the square of the number
return number * number;
}
static void Main(string[] args)
{
// Call static method using class name
int result = Geeks.Square(5);
Console.WriteLine("Square of 5 is: " + result);
}
}
Output
Square of 5 is: 25
Method Parameters
Methods can accept parameters to perform operations based on input values. Below is a table summarizing different types of parameters:
Parameter | Description | Example |
---|---|---|
Value | Passes a copy of the argument | void Display(int x) |
Reference | Passes a reference to the argument | void Update(ref int x) |
Output | Parameter Used to return multiple values | void GetValues(out int x) |
Example :
// Method Parameters
using System;
class Geeks
{
// default parameter
static void Display(int x) {
Console.WriteLine("Value Parameter: " + x);
}
// reference parameter
static void Update(ref int y) {
// Modify the original variable
y += 5;
Console.WriteLine("Reference Parameter: " + y);
}
// output parameter
static void GetValues(out int z) {
// Assign value to output parameter
z = 20;
Console.WriteLine("Output Parameter: " + z);
}
// Main Method
static void Main(string[] args)
{
int value = 10;
// Call method with value parameter
Display(value);
// Call method with reference
// parameter
Update(ref value);
int outputValue;
// Call method with
// output parameter
GetValues(out outputValue);
}
}
Output
Value Parameter: 10 Reference Parameter: 15 Output Parameter: 20
Characteristics of Instance Methods
- Belong to an Object: Instance methods require an object of the class to be called.
- Access to Instance Variables: They can access and modify instance variables and other instance methods directly.
- Dynamic Binding: Instance methods can be overridden in derived classes, allowing for polymorphic behavior in each instance method that resides within the memory allocated for that specific object.
Advantages and Disadvantages
There are many advantages to using methods. Some of them are listed below:
- It makes the program well structured.
- Methods enhance the readability of the code.
- It provides an effective way for the user to reuse the existing code.
Limitations of Methods
- Performance Overhead: Each method call adds a slight delay due to stack frame creation.
- Limited Context: Methods may require additional parameters to access class-level data, increasing code complexity.
- Debugging Complexity: Nested or numerous method calls can make debugging more difficult