Cover by Morgan Skinner, Karli Watson, Jay Glynn, Bill Evjen, Christian Nagel

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Chapter 6

Arrays and Tuples

WHAT’S IN THIS CHAPTER?

  • Simple arrays
  • Multidimensional arrays
  • Jagged arrays
  • The Array class
  • Arrays as parameters
  • Enumerations
  • Tuples
  • Structural comparison

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

The wrox.com code downloads for this chapter are found at http://www.wrox.com/remtitle.cgi?isbn=1118314425 on the Download Code tab. The code for this chapter is divided into the following major examples:

  • SimpleArrays
  • SortingSample
  • ArraySegment
  • YieldDemo
  • StructuralComparison

MULTIPLE OBJECTS OF THE SAME AND DIFFERENT TYPES

If you need to work with multiple objects of the same type, you can use collections (see Chapter 10, “Collections”) and arrays. C# has a special notation to declare, initialize, and use arrays. Behind the scenes, the Array class comes into play, which offers several methods to sort and filter the elements inside the array. Using an enumerator, you can iterate through all the elements of the array.

To use multiple objects of different types, the type Tuple can be used. See the “Tuples” section later in this chapter for details about this type.

SIMPLE ARRAYS

If you need to use multiple objects of the same type, you can use an array. An array is a data structure that contains a number of elements of the same type.

Array Declaration

An array is declared by defining the type of elements inside the array, followed by empty brackets and a variable name. For example, an array containing integer elements is declared like this:

int[] myArray;

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required