In C# (tested with .NET 8 / VS 2022), using a single parenthesized enum constant pattern with a when guard inside a switch expression produces CS0426, as if the enum member were a nested type, even though the same enum member works in other pattern contexts.
Small demonstration repo code:
// default for .NET 8 console template
// Microsoft Visual Studio Community 2022 Version 17.14.21+36717.8.-november.2025-
// Microsoft .NET Framework Version 4.8.09037
// Test_01 produces three errors
// [EnumTwo] CS0426: The type name 'EnumTwo' does not exist in the type 'CustomEnum'
// [when] CS0103: The name 'when' does not exist in the current context
// [flag] CS1003: Syntax error, '=>' expected
// Test_02, Test_03, Test_04, Test_05 have no errors.
// Therefor the problematic shape is specifically (EnumType.EnumMember) when <guard> in a switch expression.
enum CustomEnum { EnumOne, EnumTwo }
class Program
{
static string Test_01(CustomEnum reqs, bool flag) =>
reqs switch
{
(CustomEnum.EnumTwo) when flag => "ok",
_ => "nope"
};
static string Test_02(CustomEnum reqs, bool flag) =>
reqs switch
{
CustomEnum.EnumTwo when flag => "ok",
_ => "nope"
};
static string Test_03(CustomEnum reqs, bool flag) =>
(reqs, true) switch
{
(CustomEnum.EnumTwo, _) when flag => "ok",
_ => "nope"
};
static string Test_04(CustomEnum reqs, bool flag) =>
reqs switch
{
(CustomEnum.EnumTwo) => "ok",
_ => "nope"
};
static string Test_05(CustomEnum reqs, bool flag) =>
((int)reqs) switch
{
(1) when flag => "ok",
_ => "nope"
};
static void Main() { }
}
In C# (tested with .NET 8 / VS 2022), using a single parenthesized enum constant pattern with a when guard inside a switch expression produces CS0426, as if the enum member were a nested type, even though the same enum member works in other pattern contexts.
Small demonstration repo code: