VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/methods-in-c-sharp/

⇱ Methods in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Methods in C#

Last Updated : 23 Apr, 2026

A method is a block of code that performs a specific task. It can take inputs, return results, and is defined within classes to make programs modular, readable and reusable.

Example: Below example shows method in C#

Declaring a Method

The method signature consists of the method name and the types of its parameters. It uniquely identifies the method within its class. The image below shows how a method signature is defined.

👁 Method
Declaring a 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:


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:


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:


Output
Value Parameter: 10
Reference Parameter: 15
Output Parameter: 20

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 are shared among all objects of the class and operate on the instance-specific data.

Advantages

  • 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

  • Method calls add slight overhead due to stack frame creation.
  • Static methods require extra parameters to access instance data.
  • Nested or multiple method calls make debugging harder.
Comment
Article Tags:

Explore