Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

goto: practical reference

goto: practical reference: goto transfers control to a label in an allowed scope or to a switch case.

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

Syntax

goto /* grammar-specific form */

goto transfers control to a label in an allowed scope or to a switch case.

Use it sparingly to exit a complex state machine when simpler structure is worse.

It complicates control-flow analysis and cannot enter a scope with uninitialized locals.

The transfer still honors finally; the label must be in the same function.

The complete program actually uses goto; its exact expected stdout is 1.

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;

var value = 1; goto Done; value = 2; Done: Console.WriteLine(value);

Result

1

The complete program actually uses `goto`; its exact expected stdout is `1`.

Sources