問題文
A method must map each value of an enum with twelve constants to a different string. Which characteristic makes a switch expression preferable to a chain of if and else if statements here?
選択肢
- A switch expression converts the enum value to an int automatically, so the comparison is done on numbers instead of on object references and cannot fail.
- A switch expression evaluates all of its arms in one pass, which makes it faster than a chain of separate tests.
- The compiler checks that every enum constant is covered and reports an error when one is missing and no default arm is present.
- A switch expression allows an arm to fall through into the next arm, which lets several enum constants share one result without repeating the code.