Write a onetotwo page report on how Cs switch statement is s

Write a one-to-two page report on how C#\'s switch statement is safer than that of C.

Solution

Switch statement or switch case in C# is litte different than in c or c++. C#\'s version of switch statement is less error prone and thus safer than that of C as C# prohibits fallthrough and generates error if you accidently forget to use break statement.

In c/C++ Switch statement transfers control to that section whose case label matches the value of the switch expression and if there is no matching case label, control is transferred to the default section. Usually each case section ends with break statement otherwise there will be fallthrough which means the control moves to the next case section. This fallthrough gets messy if you forget using break statement and can result in major errors and unexpected output. C# does not allow execution to continue from one switch section to the next and prohibits this fall-through and allows only when the body of the case is empty. For example

Here break statement is neccessary if the switch section is not empty otherwise there will be an error which is not the case with c.

Write a one-to-two page report on how C#\'s switch statement is safer than that of C.SolutionSwitch statement or switch case in C# is litte different than in c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site