Array
We learned about arrays in C# in a previous lesson
, so let’s spend a moment comparing them to Lists
.
Lists
are generally easier to use than C#’s Array
. Let’s see why this is.
Why does C# have both Arrays
and Lists
? The answer is historical, at least in part. C# is a C-style language, and arrays are the most basic data structure in C.
Using an Array
over a List
might be preferred in some circumstances, primarily for performance reasons (array operations are generally faster than List
operations). Also note that Arrays
are of fixed size. You cannot expand or contract an Array
after it is created, so you must know exactly how many elements it will need to hold when you create it. This fact is reason enough to use Lists
in most scenarios.
To illustrate Array
usage, here is a version of the Gradebook
program using Arrays
instead of Lists
. The ArrayGradebook
project is in csharp-web-dev-examples
repository.
|
|
The ArrayGradebook
project lives in the csharp-web-dev-controlflowandcollections
repository. If you haven’t forked and cloned the repository, you should do so now.
Note that we have to decide up front how large our arrays, students
and grades
, are going to be. Thus, this program sets an arbitrary maximum amount of students, likely larger than any user will enter. It may seem obvious, then, that Array
has no equivalent Add()
method. The only way to access and alter an element in an Array
is with bracket notation, using an explicit index. For example, gradebook defines a counter variable, numStudents
. When the first student is entered by the user, the value is stored in newStudent. If the value is not the empty string, then the value in students at position 0 is assigned the newStudent
value. The next time the do-while
loop executes, the value of students
at position 1 will be assigned. This process continues until the user enters an empty string for newStudent
. Because we must always access and assign Array
elements using an explicit index, our code can seem littered with Array
counter variables (like our friends i
and j
), making it more difficult to read (not to mention more error-prone).
Like Lists
, however, we can loop through an Array
using a foreach
loop as long as we don’t need to use the index of the current item. If we only wanted to print each student’s name, and not their grade, at the end of our program, we could do the following:
|
|
We’ll use Arrays
in C# from time-to-time, but for the most part you should rely on Lists
to store collections of values, or ordered data.
Array
size and element values cannot be changed once defined.
- True
- False
Given the Array
below, which of the following options is a valid action?
int[] randomNumbers = new int[5];
randomNumbers.Add(3);
randomNumbers.Add("one");
randomNumbers[0] = "three";
randomNumbers[0] = 1;