Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

recursion: practical reference

recursion: practical reference: Recursion solves a problem by calling itself on a smaller case and defining a terminating base case.

Version: C# language and .NET 10 reference runtime

Syntax

// recursion

Recursion solves a problem by calling itself on a smaller case and defining a terminating base case.

Use it for natural trees and divide-and-conquer algorithms at controlled depth.

C# does not guarantee tail-call elimination; deep recursion can overflow the stack.

A missing or wrong base case causes endless calls; cycles require a visited set.

The complete program actually uses recursion; its exact expected stdout is 120.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

Console.WriteLine(Factorial(5)); static int Factorial(int value) => value <= 1 ? 1 : value * Factorial(value - 1);

Result

120

The complete program actually uses `recursion`; its exact expected stdout is `120`.

Sources