04 Feb C# Methods
A method in C# is an organized block of reusable code, which avoids repeating the tasks again and again. If you want to do a task repeatedly in a code, then just make a method, set the task in it, and call the multiple times whenever you need it. These are also called Functions.
We have discussed the following topics:
- Create a Method
- Call a Method
- Method Parameters
- Multiple Parameters
- Recursion
- Recursion Example
- Create and call a Method
Create a Method
To create a method in C#, set the name of the function followed by parentheses. We have set the access specifier and return type as well. Let us see an example syntax:
1 2 3 4 5 6 7 8 9 |
class Studyopedia { static void Example() { // code } } |
The static keyword suggests the method belongs to the Studyopedia class. It is not an object of the Studyopedia class.
Call a Method
To call a method in C#, just write the method name as shown below. A method in C# gets executed when it is called:
1 2 3 |
Example(); |
Let us now see an example to create and call a Method in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; namespace Demo { class Studyopedia { static void Example() { Console.WriteLine("Inside the Function"); } static void Main(string[] args) { // Calling the function we created above Example(); Example(); } } } |
Output
1 2 3 4 |
Inside the Function Inside the Function |
Method Parameters
Set the parameters in a C# method after the name of the method. For example:
1 2 3 |
void Example(int rank) {} |
To call a function with a parameter, set the value of the variable while calling, for example:
1 2 3 |
Example(5) |
Therefore, we have passed the rank with the value 2 while calling above.
Let us now see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; namespace Demo { class Studyopedia { static void Example(int rank) { Console.WriteLine("Inside the Function"); Console.WriteLine("Rank = {0}",rank); } static void Main(string[] args) { // Calling the function we created above // A parameter is also passed while calling Example(5); } } } |
Output
1 2 3 4 |
Inside the Function Rank = 5 |
Multiple Parameters
We can also set more than one parameter in a function in C#. For example:
1 2 3 |
void Example(string player, int rank, int points) { } |
To call a function with multiple parameters, set the value of the variable while calling, for example:
1 2 3 |
Example("Amit", 1, 90) |
We have passed the following multiple parameters while calling above:
- player: Amit
- rank: 1
- points: 90
Let us now see an example to implement multiple parameters for methods in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; namespace Demo { class Studyopedia { // Function with multiple parameters static void Example(string player, int rank, int points) { Console.WriteLine("Player = {0}",player); Console.WriteLine("Rank = {0}",rank); Console.WriteLine("Points = {0}",points); Console.Write("\n"); } static void Main(string[] args) { // Calling the function we created above // We have passed multiple parameters while calling Example("Amit", 1, 90); Example("David", 2, 86); Example("Tom", 3, 80); Example("Adam", 4, 78); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Player = Amit Rank = 1 Points = 90 Player = David Rank = 2 Points = 86 Player = Tom Rank = 3 Points = 80 Player = Adam Rank = 4 Points = 78 |
Recursion in C#
In C#, when a function calls itself, it is called Recursion. In another sense, with Recursion, a defined function can call itself. Recursion is a programming approach, which makes code efficient and reduces LOC.
The following figure demonstrates how recursion works when we calculate Factorial in C# with Recursion:
Recursion Example
Let us now see how to find the factorial of a number in C# with Recursion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; namespace Demo { class Studyopedia { // A custom method to calculate factorial static int FactMethod(int n) { if (n >= 1) { return n*FactMethod(n-1); // Recursive Calls } else { return 1; // Factorial 0 is 1 } } static int Main(string[] args) { int res = FactMethod(5); Console.WriteLine("Factorial = {0}",res); return 0; } } } |
Output
1 2 3 |
Factorial = 120 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments