8.1 a) arrays. b) variables, type. c) foreach
. d) index (or position number). e) two-dimensional. f) foreach (double d in numbers)
. g) an array of string
s, usually called args
. h) args.Length
. i) test
. j) params
modifier. k) getter-only.
8.2 The answers to Self-Review Exercise 8.2 are:
False. An array can store only values of the same type.
False. An array index must be an integer or an integer expression.
For individual value-type elements of an array: False. A called method receives and manipulates a copy of the value of such an element, so modifications do not affect the original value. If the reference of an array is passed to a method, however, modifications to the array elements made in the called method are indeed reflected in the original. For individual elements of a reference type: True. A called method receives a copy of the reference of such an element, and changes to the referenced object will be reflected in the original array element.
False. Command-line arguments are separated by whitespace.
False. As of C# 6, you can use auto-property initialization in auto-implemented property declarations.
8.3 The answers to Self-Review Exercise 8.3 are:
const int ArraySize = 10;
var fractions = new double[ArraySize];
fractions[3]
fractions[9] = 1.667;
fractions[6] = 3.333;
var total = 0.0;
for (var x = 0; x < fractions.Length; ++x)
{
total += fractions[x];
}
var total = 0.0;
foreach (var element in fractions)
{
total += element;
}
8.4 The answers to Self-Review Exercise 8.4 are:
var table = new int[ArraySize, ArraySize];
Nine.
for (var x = 0; x < table.GetLength(0); ++x)
{
for (var y = 0; y < table.GetLength(1); ++y)
{
table[x, y] = x + y;
}
}
8.5 The answers to Self-Review Exercise 8.5 are:
Error: Assigning a value to a constant after it’s been initialized.
Correction: Assign the correct value to the constant in the const
declaration.
Error: Referencing an array element outside the bounds of the array (b[10]
).
Correction: Change the <=
operator to <
.
Error: Array indexing is performed incorrectly.
Correction: Change the statement to a[1, 1] = 5;
.