Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

case: odniesienie praktyczne

case: odniesienie praktyczne: case definiuje etykietę wartości lub wzorca w instrukcji switch.

Wersja: C# language and .NET 10 reference runtime

Składnia

case /* grammar-specific form */

case definiuje etykietę wartości lub wzorca w instrukcji switch.

Stosuj do jawnych wariantów z opcjonalnym when.

Każda sekcja switch musi kończyć się transferem sterowania, takim jak break, return, throw lub goto; kilka ułożonych kolejno etykiet case może współdzielić jedną sekcję.

Kolejność wzorców ma znaczenie, a zasłonięty case jest błędem kompilacji.

Kompletny program rzeczywiście używa case; oczekiwany stdout to dokładnie two.

Przykłady

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; }

Wynik

two

Kompletny program rzeczywiście używa `case`; oczekiwany stdout to dokładnie `two`.

Źródła