Array instances occupy space in memory. Like objects, arrays are created with keyword new. To create an array instance, you specify the type and the number of array elements and the number of elements as part of an array-creation expression that uses keyword new. Such an expression returns a reference that can be stored in an array variable. The following declaration and array-creation expression create an array object containing 12 int elements and store the array's reference in variable c:
int[] c = new int[ 12 ];
This expression can be used to create the array shown in Fig. 8.1 (but not the initial values in the arraywe'll show how to initialize the elements of an array momentarily). This task also can be performed in two steps as follows:
int[] c; // declare the array variable c = new int[ 12 ]; // create the array; assign to array variable
In the declaration, the square brackets following the variable type int indicate that c is a variable that will refer to an array of ints (i.e., c will store a reference to an array object). In the assignment statement, the array variable c receives the reference to a new array object of 12 int elements. When an array is created, each element of the array receives a default value0 for the numeric simple-type elements, false for bool elements and null for references. As we will soon see, we can provide specific, nondefault initial element values when we create an array.
|
An application can create several arrays in a single declaration. The following declaration reserves 100 elements for string array b and 27 elements for string array x:
string[] b = new string[ 100 ], x = new string[ 27 ];
In this declaration, string[] applies to each variable in the declaration. For readability, we prefer to declare only one variable per declaration, as in:
string[] b = new string[ 100 ]; // create string array b string[] x = new string[ 27 ]; // create string array x
|
An application can declare arrays of value-type elements or reference-type elements. For example, every element of an int array is an int value, and every element of a string array is a reference to a string object.
Examples Using Arrays |
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index