简体   繁体   中英

C# Pattern matching with an or condition

Is it possible to use an OR condition within a if statement when using pattern matching?

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. Is it possible to achieve this without having to write a switch statement or another if statement?

You can test if viewModel is one of two classes like so:

if (viewModel is StudentViewModel || viewModel is ParentViewModel) {
  // ... viewModel is either a StudentViewModel  or a ParentViewModel
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM