Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

return: practical reference

return: practical reference: return ends a function and can pass a result to its caller.

Version: C# language; verified against C# 14 reference

Syntax

return /* grammar-specific form */

return ends a function and can pass a result to its caller.

Use early return to keep guard clauses and the main path simple.

Code after an unconditional return is unreachable, although finally still runs.

An async method returning Task or Task reports completion or a result through that Task; async may also return void, a suitable task-like type such as ValueTask or ValueTask, or IAsyncEnumerable for an async iterator. An ordinary iterator cannot use return with a value.

The complete program actually uses return; its exact expected stdout is 8.

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(Double(4)); static int Double(int value) { return value * 2; }

Result

8

The complete program actually uses `return`; its exact expected stdout is `8`.

Sources