I
I
INTERNALINTERFERENCE2021-11-03 14:41:09
C++ / C#
INTERNALINTERFERENCE, 2021-11-03 14:41:09

Why doesn't C# support this pattern matching?

why can't I write like this:
var matched = obj is { Id: variable }
are there any libraries that will allow me to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-11-03
@INTERNALINTERFERENCE

C# just doesn't know how to use variables in pattern matching and switch-case.
In this case, you can write like this: Well, or like this, if the case is more complex:
var matched = obj != null && obj.Id == variable;

var matched = obj is { Id: var id} && id == variable;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question