CKTIS / C#
when: praktická referencia
when: praktická referencia: when pridáva filter ku catch alebo guard k vzoru switch.
Verzia: C# language; verified against C# 14 reference
Syntax
when /* grammar-specific form */when pridáva filter ku catch alebo guard k vzoru switch.
Použite ho na zúženie prípadu bez zachytenia nevyhovujúcej výnimky.
Filter má byť bezpečný a bez vedľajších účinkov; false pokračuje v hľadaní handlera.
Výnimka vyhodená filtrom výnimiek sa považuje za false.
Úplný program skutočne používa when; presne očakávaný stdout je filtered.
Príklady
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;
try { throw new ArgumentException("x"); } catch (ArgumentException error) when (error.Message.StartsWith("x")) { Console.WriteLine("filtered"); }
Výsledok
filtered
Úplný program skutočne používa `when`; presne očakávaný stdout je `filtered`.