Open In App

C# Methods

Last Updated : 11 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

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:

C#
usingSystem;classGeeks{// Instance method to display a messagepublicvoidDisplayMessage(){Console.WriteLine("Hello from the Display "+"Message method!");}staticvoidMain(string[]args){Geeksgeek=newGeeks();// Create an instance of Geeks// Call the instance method// directlygeek.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:

C#
usingSystem;classGeeks{// Static method to calculate// square of a numberpublicstaticintSquare(intnumber){// Return the square of the numberreturnnumber*number;}staticvoidMain(string[]args){// Call static method using class nameintresult=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 :

C#
// Method ParametersusingSystem;classGeeks{// default parameterstaticvoidDisplay(intx){Console.WriteLine("Value Parameter: "+x);}// reference parameterstaticvoidUpdate(refinty){// Modify the original variabley+=5;Console.WriteLine("Reference Parameter: "+y);}// output parameterstaticvoidGetValues(outintz){// Assign value to output parameterz=20;Console.WriteLine("Output Parameter: "+z);}// Main MethodstaticvoidMain(string[]args){intvalue=10;// Call method with value parameterDisplay(value);// Call method with reference// parameterUpdate(refvalue);intoutputValue;// Call method with// output parameterGetValues(outoutputValue);}}

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


Next Article

Similar Reads

close