How to solve “Local variable 'touch' might not be initialized before accessing” in a switch statement with property pattern

舒康 段 21 Reputation points
2021-12-05T16:17:14.383+00:00

155085-qq%E5%9B%BE%E7%89%8720211206001224.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,918 questions
{count} votes

Accepted answer
  1. Viorel 117.3K Reputation points
    2021-12-05T16:58:01.45+00:00

    Maybe solve it in this manner:

    case { device: Touchscreen }:
    case { device: Mouse }:
        var a = x.device;
        . . .
    

    or

    case { device: Mouse or Touchscreen }:
        var a = x.device;
        . . .
    

    where x is the object that is currently analysed.

    Probably it depends on other details.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.