Is it possible to use pattern matching here? - C#