Why is it that if you declare a return type on a method, that you have to specify a return in the scope of that method? Meaning, if I have a If/Switch in my method I can’t specify the return there “ALONE” and not in the main method body? Is the only way around this is to assign the value returned by the nested procedure to a variable and return the variable in the main body of the method?
Example:
public Int GetNum()
{
var a = 1;
switch(a)
{
case 1:
return 1 + 5;
default:
return a;
}
return a; //Why must I specify this return if it is going to hit my switch statement
//and return some int.
}
If you have a
default, that’s not necessary.If you don’t have a
default, you need an outerreturnin case none of thecases execute.