Understanding Arrays in C#: A Complete Guide with Examples and Types
Arrays are an essential part of C# programming. They provide a way to store multiple values of the same type in a single variable, making data management more efficient. In this blog, we will explore the concept of arrays in C#, covering their types, declaration, initialization, and operations with practical examples.
1. What are Arrays in C#?
An array is a collection of variables of the same type that are stored at contiguous memory locations. Arrays allow you to work with multiple data values under a single name, making it easier to perform operations like searching, sorting, and iterating over data.
In C#, arrays are zero-indexed, meaning the first element is stored at index 0, the second at index 1, and so on. They can store values of primitive types like int
, double
, or objects of a custom class.
2. Declaring and Initializing Arrays
Declaring an array in C# involves specifying the type of data it will hold and its size. Here's how to declare and initialize arrays:
// Declare and initialize an array of integers
int[] numbers = new int[5];
// Assign values to the array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Declare and initialize an array in one line
string[] names = {"Alice", "Bob", "Charlie"};
3. Types of Arrays in C#
C# supports different types of arrays based on use cases:
- Single-dimensional arrays: The most common type, storing elements in a linear form.
- Multi-dimensional arrays: Used for matrices or tabular data.
- Jagged arrays: Arrays of arrays where each array can have a different size.
Example: Multi-dimensional Array
// Declare a 2D array (multi-dimensional)
int[,] matrix = new int[2, 3] {
{1, 2, 3},
{4, 5, 6}
};
// Access an element
int value = matrix[1, 2]; // Outputs 6
Example: Jagged Array
// Declare a jagged array
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[] {1, 2, 3};
jaggedArray[1] = new int[] {4, 5};
// Access elements
int firstValue = jaggedArray[0][1]; // Outputs 2
int secondValue = jaggedArray[1][0]; // Outputs 4
4. Common Operations on Arrays
Here are some common operations you can perform on arrays in C#:
- Iteration: Use
for
orforeach
loops to traverse the array. - Sorting: Use
Array.Sort()
to sort elements. - Length: Use the
Length
property to get the size of the array.
Example: Iterating Over an Array
int[] numbers = {10, 20, 30, 40};
// Using a for loop
for (int i = 0; i < numbers.Length; i++) {
Console.WriteLine(numbers[i]);
}
// Using a foreach loop
foreach (int num in numbers) {
Console.WriteLine(num);
}
conclusion
---------------------------------------------------------------------------------------------- This Content Sponsored by Buymote Shopping app BuyMote E-Shopping Application is One of the Online Shopping App Now Available on Play Store & App Store (Buymote E-Shopping) Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8 Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication ---------------------------------------------------------------------------------------------
0 Comments