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 ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.