arrays_Creation.md - brainchildservices/curriculum GitHub Wiki
Slide 1
Array Creation
To declare an array in C#, you can use the following syntax −
datatype[] arrayName;
where,
- datatype is used to specify the type of elements in the array.
- specifies the rank of the array. The rank specifies the size of the array.
- arrayName specifies the name of the array.
For example,
double[] balance;
Slide 2
Initializing an Array
Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array.
Array is a reference type, so you need to use the new keyword to create an instance of the array. For example,
double[] balance = new double[10];
Slide 3
Assigning Values to an Array
You can assign values to individual array elements, by using the index number, like −
double[] balance = new double[10];
balance[0] = 4500.0;
You can assign values to the array at the time of declaration, as shown −
double[] balance = { 2340.0, 4523.69, 3421.0};
You can also create and initialize an array, as shown −
int [] marks = new int[5] { 99, 98, 92, 97, 95};
Slide 4
You may also omit the size of the array, as shown −
int [] marks = new int[] { 99, 98, 92, 97, 95};
You can copy an array variable into another target array variable. In such case, both the target and source point to the same memory location −
int [] marks = new int[] { 99, 98, 92, 97, 95};
int[] score = marks;
When you create an array, C# compiler implicitly initializes each array element to a default value depending on the array type. For example, for an int array all elements are initialized to 0.