C# Arrays

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.

Course Video
Course Video In English
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.

Frequently Asked Questions

Still have a question?

Let's talk

An array in C# is a collection of elements of the same data type, stored in contiguous memory locations. Arrays are used to manage multiple values efficiently.

An array is declared using square brackets, for example:
int[] numbers;
You can also initialize it with values:
int[] numbers = { 1, 2, 3, 4 };

C# supports single-dimensional arrays, multi-dimensional arrays (e.g., 2D arrays), and jagged arrays (arrays of arrays).

If an array is initialized but not assigned specific values, its elements are assigned default values based on the data type (e.g., 0 for integers, false for booleans, and null for reference types).

No, the size of an array in C# is fixed once it is declared. If you need a resizable collection, consider using a data structure like List<T>.

A multi-dimensional array has a fixed grid-like structure (e.g., a matrix), while a jagged array allows arrays with varying lengths for each dimension.

C# provides a variety of array functions (methods) that help with array manipulation and data processing. Common array functions include:

  1. Array.Sort(): Sorts elements in an array.
  2. Array.Reverse(): Reverses the order of elements in an array.
  3. Array.IndexOf(): Finds the index of a specific element in the array.
  4. Array.Copy(): Copies a portion or entire array to another array.
  5. Array.Resize(): Changes the size of an array.

You can use the Array.Sort() function to sort the elements of an array. This function sorts the elements in ascending order by default.

Yes, Iqra Academy offers free video tutorials in Hindi that explain how to use array functions in C# with practical examples. These tutorials are designed for beginners to understand basic array operations and their use cases.

Array functions in C# simplify and optimize many operations you might need to perform on arrays. Functions like sorting, searching, reversing, and resizing arrays help manage data effectively and improve code efficiency, especially when dealing with large datasets.

Yes, you can access a free online course at Iqra Academy, which provides comprehensive lessons on C# array functions. The course includes video tutorials, example codes, and hands-on exercises in Hindi for beginners.

C# array functions are widely used in many applications such as:

  • Sorting data (e.g., sorting a list of names or scores).
  • Searching for specific data (e.g., finding a user’s information).
  • Data manipulation (e.g., reversing an array to display items in reverse order).
  • Efficient memory management (e.g., resizing an array to optimize storage).