#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())

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: