Lecture 6 C Array
Lecture 6 C Array
Lecture 6
INTERMEDIATE PROGRAMMING
• 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.
• 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.
• Default values of numeric array and reference type elements are set to be respectively zero and null.
• 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