Using Arrays As Return Values from Methods

C# allows a method to return an array object reference back to the caller. Figure 10.12 shows the relevant parts of a source program making use of this feature. Two methods are defined here—Main (lines 5–40) and GetTenRandomNumbers (lines 50–71). The latter will, when called, return a reference to an array containing ten random numbers. These numbers could be used for various mathematical analyses (irrelevant for this demonstration and therefore not shown) performed in the Main method.

Figure 10.12. Returning a reference to an array object of base type int.

Recall that the return type for a method is specified ...

Get C# Primer Plus 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.