c# 7.0: switch on System.Type

后端 未结 9 2006
既然无缘
既然无缘 2020-12-03 04:10

No existing question has an answer to this question.

In c# 7, can I switch directly on a System.Type?

When I try:

    switch (Type)         


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 04:37

    Adding to the above answer, if you do not need the value to be used inside the case statement, you can use _. This syntax can be used to remove an unused variable warning message.

    switch (this.value) {
      case int _:
        //Do something else without value.
        break;
      case decimal decimalValue:
        this.value = Math.Max(Math.Min(decimalValue, Maximum), Minimum);
        break;
    }
    

提交回复
热议问题