Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

case: practical reference

case: practical reference: case defines a value or pattern label in a switch statement.

Version: C# language and .NET 10 reference runtime

Syntax

case /* grammar-specific form */

case defines a value or pattern label in a switch statement.

Use it for explicit variants with an optional when guard.

Every switch section must end with a control transfer such as break, return, throw, or goto; multiple stacked case labels can share one section.

Pattern order matters, and a shadowed case is a compile-time error.

The complete program actually uses case; its exact expected stdout is two.

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 = 2; switch (value) { case 1: Console.WriteLine("one"); break; case 2: Console.WriteLine("two"); break; }

Result

two

The complete program actually uses `case`; its exact expected stdout is `two`.

Sources