![]() |
VOOZH | about |
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#
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 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.
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:
Hello from the DisplayMessage method!
Static methods belong to the class itself rather than any specific instance. They can be called without creating an object of the class. Example:
Square of 5 is: 25
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:
Value Parameter: 10 Reference Parameter: 15 Output Parameter: 20