Switch statement without default when dealing with enumerations

后端 未结 10 2365
萌比男神i
萌比男神i 2020-12-15 15:59

This has been a pet peeve of mine since I started using .NET but I was curious in case I was missing something. My code snippet won\'t compile (please forgive the forced nat

10条回答
  •  独厮守ぢ
    2020-12-15 16:11

    In addition to the case of, you can cast any int to your enum and have an enum you aren't handling. There is also the case where, if the enum is in an external .dll, and that .dll is updated, it does not break your code if an additional option is added to the enum (like Yes, No, Maybe). So, to handle those future changes you need the default case as well. There is no way to guarantee at compile time that you know every value that enum will have for it's life.

提交回复
热议问题