O'Reilly logo

Learning C# 2005, 2nd Edition by Brian MacDonald, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10: Arrays

Quiz

Solution to Question 10–1.

6. Arrays always begin with index (or offset) zero.

Solution to Question 10–2.

Yes. Every array declares the type of objects it will hold. You can undermine the type-safety by creating an array of objects (which will hold anything, because everything derives from objects), but that is not advised.

Solution to Question 10–3.

Arrays are reference types and created on the heap.

Solution to Question 10–4.

If the elements of the array are of value types, they are created in the allocated memory for the array; otherwise, they are created elsewhere on the heap and a reference is stored in the array.

Solution to Question 10–5.

You can explicitly call new or just imply the size of the array. For example, if you have three employee objects named moe, larry, and curley:

Employee[] myEmpArray = new Employee[3] = { moe, larry, curley };

or:

Employee[] myEmpArray = { moe, larry, curley };
Solution to Question 10–6.

Allows you to pass in an indefinite number of parameters, all of the same type, which will be treated as an array. You can, if you wish, also pass in an array.

Solution to Question 10–7.

A rectangular array is a multidimensional array; a jagged array is an array of arrays.

Exercises

Solution to Exercise 10-1.

Declare a Dog class with two private members: weight (an int), and name (a string). Be sure to add properties to access the members. Then create an array that holds three Dog objects (Milo, 26 pounds; Frisky, 10 pounds; and Laika, 50 pounds). Output ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required