[英]C# Pattern matching with an or condition
Is it possible to use an OR condition within a if statement when using pattern matching?使用模式匹配时,是否可以在 if 语句中使用 OR 条件?
I have a pattern match statement like below which works:我有一个如下所示的模式匹配语句:
if (viewModel is StudentViewModel pageModel)
{
}
I would like to check if viewModel is either a StudentViewModel or ParentViewModel.我想检查 viewModel 是 StudentViewModel 还是 ParentViewModel。 Is it possible to achieve this without having to write a switch statement or another if statement?
是否可以在不必编写 switch 语句或另一个 if 语句的情况下实现这一点?
You can test if viewModel is one of two classes like so:您可以测试 viewModel 是否是两个类之一,如下所示:
if (viewModel is StudentViewModel || viewModel is ParentViewModel) {
// ... viewModel is either a StudentViewModel or a ParentViewModel
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.