|
|
Contents
Overview 1
Overview of Arrays 2
Creating Arrays 11
Using Arrays 18
Lab 6: Creating and Using Arrays 31
Review 42
Module 6: Arrays
This course is based on the prerelease Beta 1 version of Microsoft? Visual Studio .NET.
Content in the final release of the course may be different from the content included in
this prerelease version. All labs in the course are to be completed with the Beta 1 version
of Visual Studio .NET.
Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
ó 2001 Microsoft Corporation. All rights reserved.
Microsoft, ActiveX, BizTalk, IntelliSense, JScript, Microsoft Press, MSDN, PowerPoint, Visual
Basic, Visual C++, Visual C#, Visual Studio, Windows, Windows NT, and Windows Media are
either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other
countries.
Other product and company names mentioned herein may be the trademarks of their respective
owners.
Module 6: Arrays 1
Overview
n Overview of Arrays
n Creating Arrays
n Using Arrays
Arrays provide an important means for grouping data. To make the most of C#,
it is important to understand how to use and create arrays effectively.
After completing this module, you will be able to:
n Create, initialize, and use arrays of varying rank.
n Use command-line arguments in a C# program.
n Understand the relationship between an array variable and an array instance.
n Use arrays as parameters for methods.
n Return arrays from methods.
2 Module 6: Arrays
u Overview of Arrays
n What Is an Array?
n Array Notation in C#
n Array Rank
n Accessing Array Elements
n Checking Array Bounds
n Comparing Arrays to Collections
This section provides an overview of general array concepts, introduces the key
syntax used to declare arrays in C#, and describes basic array features such as
rank and elements. In the next section, you will learn how to define and use
arrays.
Module 6: Arrays 3
What Is an Array?
n An Array Is a Sequence of Elements
l All elements in an array have the same type
l Structs can have elements of different types
l Individual elements are accessed using integer indexes
Integer index 0
(zero)
Integer index 4
(four)
There are two fundamental ways to group related data: structures (structs) and
arrays.
n Structures are groups of related data that have different types.
For example, a name (string), age (int), and gender (enum) naturally group
together in a struct that describes a person. You can access the individual
members of a struct by using their field names.
n Arrays are sequences of data of the same type.
For example, a sequence of houses naturally group together to form a street.
You can access an individual element of an array by using its integer
position, which is called an index.
Arrays allow random access. The elements of an array are located in contiguous
memory. This means a program can access all array elements equally quickly.
4 Module 6: Arrays
Array Notation in C#
n You Declare an Array Variable by Specifying:
l The element type of the array
l The rank of the array
l The name of the variable
This specifies the rank of the array
This specifies the name of the array variable
This specifies the element type of the array
ttyyppee[[ ]] nnaammee;;
You use the same notation to declare an array that you would use to declare a
simple variable. First, specify the type, and then specify the name of the
variable followed by a semicolon. You declare the variable type as an array by
using square brackets. Many other programming languages, such as C and C++,
also use square brackets to declare an array. Other languages, like Microsoft?
Visual Basic?, use parentheses.
In C#, array notation is very similar to the notation used by C and C++,
although it differs in two subtle-but-important ways:
n You cannot write square brackets to the right of the name of the variable.
n You do not specify the size of the array when declaring an array variable.
The following are examples of allowed and disallowed notation in C#:
type[ ]name; // Allowed
type name[ ]; // Disallowed in C#
type[4] name; // Also disallowed in C#
Module 6: Arrays 5
Array Rank
n Rank Is Also Known as the Array Dimension
n The Number of Indexes Associated with Each Element
Rank 1: One-dimensional
Single index associates with
each long element
Rank 2: Two-dimensional
Two indexes associate with
each int element
lloonngg[[ ]] rrooww;; iinntt[[,,]] ggrriidd;;
To declare a one-dimensional array variable, you use unadorned square brackets
as shown on the slide. Such an array is also called an array of rank 1 because
one integer index associates with each element of the array.
To declare a two-dimensional array, you use a single comma inside the square
brackets, as shown on the slide. Such an array is called an array of rank 2
because two integer indexes associate with each element of the array. This
notation extends in the obvious way: each additional comma between the
square brackets increases the rank of the array by one.
You do not include the length of the dimensions in the declaration for an array
variable. |
|