#597 – Returning an Array from a Method

You can return an array from a method just like you can return any other reference-typed object from a method.  The array will be instantiated within the method and a reference to the new object is what is returned to the calling code.

Assume that we have a method declared like this in a Dog class:

public string[] FavoriteToys()

We can then call this method as follows:

            Dog kirby = new Dog("Kirby", 15);

            string[] favToys = kirby.FavoriteToys();

            foreach (string toy in favToys)

We could also just use the method call in the body of the foreach loop:

            foreach (string toy in kirby.FavoriteToys())