Arrays
In simple terms, an array is a collection of elements of the same type, stored in contiguous memory locations, and each element can be accessed using an index. The index always starts from 0, meaning the first element is at index 0, the second at index 1, and so on.
Array Declaration in C#
You can declare arrays in C# using the following syntax:
int[ ] array1; // Declares an array of integers
double[ ] array2; // Declares an array of doubles
bool[ ] array3; // Declares an array of booleans
Once declared, you can initialize arrays with a specific size or directly with elements:
// Array with a defined size but no elements yet
int[ ] array1 = new int[5]; // array1 can hold 5 integers
// Array initialized with specific elements
int[ ] array2 = new int[] { 4, 5, 1, 7, 3 }; // array2 has 5 elements
bool[ ] array3 = { true, false, true, false, true }; // array3 has 5 elements
You can also simplify the declaration by skipping the new int[ ] part when directly initializing the array with elements:
int[ ] array2 = { 4, 5, 1, 7, 3 }; // Same as above but simplified and these brackets [ ] are very important to create an array
Understanding the Array Declaration
- int[ ] array1 = new int[5];
This declares an integer array named array1 with 5 elements, but no values have been assigned yet (all components have been initialized to 0). - int[ ] array2 = new int[ ] { 4, 5, 1, 7, 3 };
This declares an integer array array2 with 5 specific elements: 4, 5, 1, 7, 3. - bool[ ] array3 = { true, false, true, false, true };
This declares a boolean array array3 with 5 specific elements: true, false, true, false, true.
Example: Creating and Accessing an Array
Let’s create a simple integer array with values and demonstrate how to access a specific element by its index.
Scenario: Create an array with values {2, 4, 10, 5, 15, 3} and print the 3rd element (index 2).
using System;
class Program
{
static void Main()
{
// Initializing an integer array
int[ ] numbers = { 2, 4, 10, 5, 15, 3 };
// Accessing and printing the 3rd element (at index 2)
Console.WriteLine($”The third element: {numbers[2]}”);
}
}
Output :
The third element: 10
Explanation:
- int[ ] numbers = { 2, 4, 10, 5, 15, 3 };
Here, we are creating an array named numbers with 6 elements: 2, 4, 10, 5, 15, 3. - WriteLine($”The third element: {numbers[2]}”);
We are accessing the 3rd element in the array by using index 2 (since array indexing starts from 0). So, numbers[2] gives 10.
Output:
The third element is 10, which is displayed in the console.
Task:
1.Integer Array:
Create an array with 5 numbers.
Fill it with the numbers 1 to 5 like this int[] variable name = {1,2,3,4,5}.
Print all the numbers of array in console using foreach loop.
2.Fruit Names Array:
Create an array to store the names of 3 fruits.
Fill it with the names of 3 fruits like -{ mango , grapes , orange }
Print all the fruit names in console using foreach loop.
3.Double Numbers Array:
Create an array with 4 decimal numbers.
Fill it with 4 decimal numbers like – {2.4 , 40.3 , 4.9}.
Print the total sum of all in console using foreach loop.
4.True/False Array:
Create an array that can store true or false values.
Fill it with 3 values: true, false, and true.
Print all the values in console using foreach loop.