8.1 Introduction

This chapter introduces the important topic of data structures—collections of related data items. Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain the same length once they’re created, although an array variable may be reassigned so that it refers to a new array of a different length.

We’ll discuss how to declare, create and initialize arrays, then show several examples of common array manipulations. We’ll present the foreach iteration statement, which provides a concise notation for accessing data in arrays (and collections, as you’ll see in Section 9.4, Introduction to Collections, and in Chapter 21, Generic Collections; Functional Programming with LINQ/PLINQ).

You’ll use the var keyword to declare implicitly typed local variables—as you’ll see, the compiler can determine a local variable’s type, based on its initializer value. We’ll introduce exception handling for detecting and processing problems that occur during an app’s execution.

Many of the chapter’s examples manipulate arrays of int simple-type values. To demonstrate that arrays also can store reference types, we’ll create a card-shuffling-and-dealing simulation that manipulates an array of Card objects. In that example, we’ll introduce C# 6’s getter-only auto-implemented properties, which define properties that can be used to get, but not set, a value.

We present two versions of an instructor GradeBook case study that use arrays to maintain sets of student grades in memory and analyze them. We’ll also demonstrate how to define methods that receive variable-length argument lists (which are implemented by C# as arrays) and a version of method Main that can receive a string array containing an app’s so-called command-line arguments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset