Summary

  • An array is an indexed collection of objects, all of the same type.

  • You declare an array by giving the type of objects the array contains, followed by the square bracket operator ([]), followed by the name of the array. You then instantiate the array with the new keyword and the number of elements the array will contain.

  • The index of the first element in the array is always zero, and the index of the last element in the array is always Length-1.

  • You can use a for loop to iterate through the array, by using the loop’s counter as the index to the array.

  • The foreach statement allows you to iterate through the items in the array (or any other collection) without the need for a counter.

  • The elements of an array can be initialized when the array is created by providing the values of the members in curly braces ({}).

  • The params keyword lets you pass an arbitrary number of parameters of the same type into a method; the method will treat the parameters as a single array.

  • Arrays can contain more than one dimension. A two-dimensional array has two indexes, which you can think of as rows and columns.

  • A rectangular array is a two-dimensional array in which all the rows have the same number of columns.

  • A jagged array is an array of arrays—the rows do not need to be all the same length.

  • The Length property of an array returns the total number of elements in the array.

  • The array class contains a number of methods for searching, sorting, and manipulating the elements.

Although arrays may seem like a simple topic more suited to discussing with the fundamentals of C#, they work equally well with objects, so we delayed showing them to you until after we’d discussed classes and methods. Arrays, along with the other collection types, are some of the easiest ways to pass a bunch of similar objects to a method. It’s time to get back into the serious object-oriented stuff, though. Back in Chapter 6, we told you that the three pillars of object-oriented programming are encapsulation, specialization, and polymorphism. You saw how encapsulation works in Chapters Chapter 7 and Chapter 8. Now the next chapter deals with both specialization and polymorphism, in the form of inheritance.

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

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