Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do we call a C# method recursively?
Recursion in C# is a programming technique where a method calls itself to solve a smaller version of the same problem. Each recursive call reduces the problem size until it reaches a base case that stops the recursion.
A recursive method must have two essential components: a base case that stops the recursion, and a recursive case that calls the method with modified parameters.
Syntax
Following is the general syntax for a recursive method −
public returnType MethodName(parameters) {
if (baseCondition) {
return baseValue;
}
else {
return MethodName(modifiedParameters);
}
}
How Recursion Works
Using Recursion for Factorial Calculation
The factorial of a number n is the product of all positive integers less than or equal to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.
Example
using System;
class Factorial {
public int Calculate(int n) {
if (n == 1 || n == 0)
return 1;
else
return n * Calculate(n - 1);
}
static void Main(string[] args) {
int value = 5;
Factorial fact = new Factorial();
int result = fact.Calculate(value);
Console.WriteLine("Factorial of {0} is: {1}", value, result);
}
}
The output of the above code is −
Factorial of 5 is: 120
Using Recursion for Fibonacci Sequence
Example
using System;
class Fibonacci {
public int Calculate(int n) {
if (n <= 1)
return n;
else
return Calculate(n - 1) + Calculate(n - 2);
}
static void Main(string[] args) {
Fibonacci fib = new Fibonacci();
Console.WriteLine("First 10 Fibonacci numbers:");
for (int i = 0; i < 10; i++) {
Console.Write(fib.Calculate(i) + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
First 10 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34
Using Recursion for String Reversal
Example
using System;
class StringReversal {
public string Reverse(string str) {
if (str.Length <= 1)
return str;
else
return str[str.Length - 1] + Reverse(str.Substring(0, str.Length - 1));
}
static void Main(string[] args) {
StringReversal sr = new StringReversal();
string original = "Hello";
string reversed = sr.Reverse(original);
Console.WriteLine("Original: " + original);
Console.WriteLine("Reversed: " + reversed);
}
}
The output of the above code is −
Original: Hello Reversed: olleH
Key Rules for Recursion
-
Base Case: Every recursive method must have a condition that stops the recursion.
-
Progress towards Base Case: Each recursive call should bring the problem closer to the base case.
-
Stack Limitation: Deep recursion can cause stack overflow exceptions if the call stack becomes too large.
-
Performance: Recursive solutions may be less efficient than iterative ones due to method call overhead.
Conclusion
Recursion in C# allows methods to call themselves to solve problems by breaking them into smaller subproblems. Every recursive method needs a base case to prevent infinite recursion and should progress toward that base case with each call. While elegant for certain problems, recursion should be used carefully to avoid stack overflow issues.
