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

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.