What’s the preferred way to handle the following case:
switch (numberOfActualBytes)
{
case 1: return something1;
case 2: return something2;
case 3: return something3;
case 4: return something4;
}
I know for certain that numberOfActualBytes due to the used contract is in range 1-4.
How should I write the code that doesn’t result in not all code paths return a value error?
I suspect I should throw some exception at the end of this function or in default switch case, but probably there is a better solution.
I prefer to throw an out-of-range exception in the
defaultcase if the application can be expected to uphold the 1..4 contract. The exception reflects the expectation on the caller that they will give me good data.If your compiler cannot figure out that the
defaultcase solves the not all code paths, then put thereturnafter theswitch. But the c# compiler will get it right.