O'Reilly logo

Learning C# 3.0 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 Solutions

Solution to Question 10-1. Arrays always begin with index (or offset) zero, so the seventh member of an array has index 6.

Solution to Question 10-2. No. Every array declares the type of objects it will hold. You can undermine this type safety by creating an array of Objects (which will hold anything, because everything derives from Object), but that is not advised.

Solution to Question 10-3. When you instantiate an array, you specify the number of elements in square brackets.

Solution to Question 10-4. Arrays are reference types and are created on the heap.

Solution to Question 10-5. The highest index in any array is always represented by Length - 1.

Solution to Question 10-6. 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 curly:

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

or:

Employee[] myEmpArray = { moe, larry, curly };

Solution to Question 10-7. There are a number of ways to iterate through the items in an array, but one of the most common is to use a for loop, using the loop’s control variable as the indexer in the array. An even simpler method is to use the foreach statement.

Solution to Question 10-8. The params keyword 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-9. A rectangular array is a multidimensional ...

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