Replies: 1 comment 2 replies
-
The compiler actually doesn't know that. (Well, the compiler might but the language doesn't.) The return type of That being said, this doesn't work even without the involvement of int i = 2;
inferred = i switch {
< 0 => new() with { Direction = Move.Left }, // error CS8754: There is no target type for 'new()'
> 0 => new() with { Direction = Move.Right }, // error CS8754: There is no target type for 'new()'
_ => new() with { Direction = Move.Nowhere }, // error CS8754: There is no target type for 'new()'
}; dotnet/csharplang#2389 was the original proposal for adding target-typing to switch expressions during C# 8. It was never formally specified, but based on the proposed spec it sounds like it would not help in this situation. As such, I'm fairly certain this would require a language change and therefore this request belongs in csharplang. As an aside: If you add ```csharp
Console.WriteLine("Hello, World!");
``` |
Beta Was this translation helpful? Give feedback.
-
[ https://dotnetfiddle.net/Widget/CKXaCr ]
Beta Was this translation helpful? Give feedback.
All reactions