
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How do we call a C# method recursively?
To call a C# method recursively, you can try to run the following code. Here, Factorial of a number is what we are finding using a recursive function display().
If the value is 1, it returns 1 since Factorial is 1.
if (n == 1) return 1;
If not, then the recursive function will be called for the following iterations if 1you want the value of 5!
Interation1: 5 * display(5 - 1); Interation2: 4 * display(4 - 1); Interation3: 3 * display(3 - 1); Interation4: 4 * display(2 - 1);
The following is the complete code to call a C# method recursively.
Example
using System; namespace MyApplication { class Factorial { public int display(int n) { if (n == 1) return 1; else return n * display(n - 1); } static void Main(string[] args) { int value = 5; int ret; Factorial fact = new Factorial(); ret = fact.display(value); Console.WriteLine("Value is : {0}", ret ); Console.ReadLine(); } } }
Output
Value is : 120
- Related Articles
- How do we call a Java method recursively?
- How can we call the invokeLater() method in Java?\n
- What do we call scientists who study rocks?
- Can we call a constructor directly from a method in java?
- How do we upsample a time-series using asfreq() method?
- Can we call a method on "this" keyword from a constructor in java?
- How to call a method after a delay?
- Why do we call cotton bolls" instead of "cotton balls"?"
- Can we call Superclass’s static method from subclass in Java?
- How do we pass parameters by value in a Java method?
- How do we pass parameters by reference in a C# method?
- How do we pass parameters by value in a C# method?
- How do we pass an array in a method in C#?
- Can we call methods of the superclass from a static method in java?
- (a) What do we call those particles which have more or less electrons than the normal atoms?(b) What do we call those particles which have more electrons than the normal atoms? (c) What do we call those particles which have less electrons than the normal atoms?

Advertisements