1 / 8

A Brief Introduction to C# Arrays

A Brief Introduction to C# Arrays. Chapter 10. 1. Objectives. You will be able to: Use arrays correctly in your C# programs. 2. Creating Arrays. In C#, arrays are reference types . Declaration does not allocate memory for the array. (Just for a reference.) You don’t declare the size.

aisha
Télécharger la présentation

A Brief Introduction to C# Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Brief Introduction to C# Arrays Chapter 10 1

  2. Objectives You will be able to: • Use arrays correctly in your C# programs 2

  3. Creating Arrays In C#, arrays are reference types. Declaration does not allocate memory for the array. (Just for a reference.) You don’t declare the size. int[ ] ID; Compare C: int ID[2000]; 3

  4. Creating Arrays You must instantiate an array with “new”. my_ints = new int[2000]; The variable my_ints is a reference If a local variable, allocated on the stack If a member variable, allocated as part of the object of which it is a member (on the heap) The actual array is allocated on the heap. 4

  5. Array Initialization By default, array elements are initialized the same as class member variables: • All numeric types 0 • Enumerations 0 (even if not a valid value) • bool false • References null • structs Apply above rules to members. 5

  6. Array Initialization You can specify inital values with the instantiation: int[ ] id = new int [4] {9, 3, 7, 2}; The number of values supplied must match the array size. Initial values do not have to be compile time constants. 6

  7. Array Initialization If you provide an initializer, you may omit the size int[ ] id = new int [ ] {9, 3, 7, 2}; or even int[ ] id = {9, 3, 7, 2}; 7

  8. Accessing Array Elements • Index values run from 0 to N-1 • (Like C) • Unlike C, using an invalid index results in an exception. int[ ] id = new int [4] {9, 3, 7, 2}; int i; i = id[4]; // This throws an IndexOutOfRange exception. 8

More Related