Return Type
The return type of a method can be either a type or void. This indicates what a caller of the method will get (under normal circumstances) in return for calling the method. When a type is specified, all execution paths through the method’s body should reach a point where the return keyword is used to hand back a result to the caller. As you see later, it’s also possible for a method to throw an exception. For example:
public static int Div(int n, int d){ if (d == 0) throw new ArgumentOutOfRangeException("d"); return n / d;}
In the preceding example, the expression n / d results in an int value that can be returned to the caller by using the return keyword. If the denominator is zero, an exception will terminate the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access