0% found this document useful (0 votes)
11 views

Lecture 6 C Array

Uploaded by

Yuri Bacarra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture 6 C Array

Uploaded by

Yuri Bacarra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C# Array

Lecture 6
INTERMEDIATE PROGRAMMING

Engr. Jennelyn P. Cabale


C# Arrays
• An array is a group of like-typed variables that are referred to by a common
name.

• Each data item is called an element of the array.

• The data types of the elements may be any valid data type like char, int, float,
etc. and the elements are stored in a contiguous location.

• Length of the array specifies the number of elements present in the array.

• The allocation of memory for the arrays is done dynamically.


Important Points to Remember About Arrays in C#
• All arrays are dynamically allocated.

• Find their length using member length.

• Array variable can also be declared like other variables with [] after the data type.

• The variables in the array are ordered and each has an index beginning from 0.

• C# array is an object of base type System.Array.

• Default values of numeric array and reference type elements are set to be respectively zero and null.

• Array elements can be of any type, including an array type.

• Array types are reference types which are derived from the abstract base type Array.
Array Declaration
• Syntax : • Example :
< Data Type > [ ] < Name_Array >
int[] x;
string[] s;
double[] d;
Student[] stud1;
Array Initialization
• Syntax :
type [ ] < Name_Array > = new < datatype > [size];

• Example:
int[] intArray1 = new int[5];
int[] intArray2 = new int[5]{1, 2, 3, 4, 5};
int[] intArray3 = {1, 2, 3, 4, 5};
Initialization of an Array after Declaration
Example: Example : Wrong Declaration for initializing
an array
// Declaration of the array
string[] str1, str2; // compile-time error: must give size of an
array
// Initialization of array int[] intArray = new int[];
str1 = new string[5]{ “Element 1”, “Element
2”, “Element 3”, “Element 4”, “Element 5” }; // error : wrong initialization of an array
string[] str1;
str2 = new string[5]{ “Element 1”, “Element str1 = {“Element 1”, “Element 2”, “Element
2”, “Element 3”, “Element 4”, “Element 5” }; 3”, “Element 4” };
Accessing Array Elements
One Dimensional Array
Multidimensional Arrays
• It is also known as a Rectangular Array in C#
• It can be a 2D-array or 3D-array or more.
• To storing and accessing the values of the array, one required the nested loop.
THE END

You might also like