0% found this document useful (0 votes)
12 views4 pages

Arrays C Sharp

C# arrays are fixed-size data structures that store multiple values of the same type, allowing efficient management of related data. They can be single-dimensional, multi-dimensional, or jagged, with various methods for accessing and manipulating elements. Arrays are dynamically allocated objects in C#, and their properties include length and rank, making them essential for managing collections in applications.

Uploaded by

ranaibrahim453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Arrays C Sharp

C# arrays are fixed-size data structures that store multiple values of the same type, allowing efficient management of related data. They can be single-dimensional, multi-dimensional, or jagged, with various methods for accessing and manipulating elements. Arrays are dynamically allocated objects in C#, and their properties include length and rank, making them essential for managing collections in applications.

Uploaded by

ranaibrahim453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C# Arrays:

Arrays are fundamental data structures in C# that allow you to store multiple values of the same
type under a single variable name. They provide efficient ways to work with collections of related
data. An array is a group of like-typed variables that are referred to by a common name. And each
data item is called an element of the array. 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.

Arrays can be initialized after the declaration. It is not necessary to declare and initialize at the
same time using the new keyword. However, Initializing an Array after the declaration, it must be
initialized with the new keyword. It can’t be initialized by only assigning values.
string[] weekDays = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

int[] primes = new int[] { 2, 3, 5, 7, 11 };

Basic Arrays in C#

A C# array is a fixed-size collection that stores elements of the same data type in contiguous
memory locations. This array contains only one row for storing the values. All values of this
array are stored alongside starting from 0 to the array size. For example, a single-dimensional
array of 5 integers :

Here's how you can declare and initialize arrays:


// Code Example

int[] numbers;

// Declaration and initialization

int[] scores = new int[5]; // Creates an array of 5 integers (default value of every
element will be 0)

string[] names = new string[3]; // Creates an array of 3 strings (default value for
every element will be null)

// Declaration with values

int[] primes = new int[] { 2, 3, 5, 7, 11 };

string[] weekDays = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

Arrays in C# have useful properties and methods:

- Length`: Gets the total number of elements


- Rank`: Returns the number of dimensions
- `GetValue()`: Returns the value at a specified position

- `SetValue()`: Sets a value at a specified position

2D Arrays

Two-dimensional arrays, often called matrices, organize data in rows and columns. They're
particularly useful for grid-based applications like spreadsheets or games.

// Declaration of a 2D array

int[,] matrix = new int[3, 4]; // 3 rows, 4 columns

// Initialization with values

int[,] grid = new int[,] {

{ 1, 2, 3, 4 },

{ 5, 6, 7, 8 },

{ 9, 10, 11, 12 }

};

// Accessing elements

int value = grid[1, 2]; // Gets the value at row 1, column 2 (value 7)

Multidimensional Arrays

C# supports arrays with more than two dimensions. These can represent complex structures like
3D spaces or multi-factor data sets. 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 multi-dimensional array
declaration, initialization and accessing is as follows :
// 3D array declaration

int[,,] cube = new int[3, 3, 3]; // 3x3x3 cube

// Initialization with values

int[,,] threeDArray = new int[,,] {

{ { 1, 2 }, { 3, 4 } },

{ { 5, 6 }, { 7, 8 } } };
// Accessing elements

int value = threeDArray[1, 0, 1]; // Gets the value at position [1,0,1] (value 6)

Jagged Arrays

Jagged arrays are arrays of arrays, where each sub-array can have a different length. The jagged
array elements may be of different dimensions and sizes. They're useful when data isn't uniformly
structured.

// Declaration

int[][] jaggedArray = new int[3][];

// Initialization of every element of jagged array

jaggedArray[0] = new int[] { 1, 2, 3 };

jaggedArray[1] = new int[] { 4, 5 };

jaggedArray[2] = new int[] { 6, 7, 8, 9 };

// Accessing elements

int value = jaggedArray[2][3]; // Gets the value 9

Array Iterating Techniques

When working with arrays in Windows Forms applications, you can iterate through them in
various ways:

// For loop (most common for all array types)

for (int i = 0; i < array.Length; i++)

// Process array[i]

}
// For each loop (simpler syntax for single-dimension arrays)

foreach (int item in array)

// Process item

// For loop for 2D arrays

for (int row = 0; row < array.GetLength(0); row++)

for (int col = 0; col < array.GetLength(1); col++)

// Process array[row, col]

Arrays are essential components in C# programming, especially in Windows Forms applications


where they help manage collections of UI elements, data for visualization, game states, and
much more.

Important Points to Remember About Arrays

1. In C#, all arrays are dynamically allocated.


2. Since arrays are objects in C#, we can find their length using member length. This is different
from C/C++ where we find length using sizeof operator.
3. A C# array variable can also be declared like other variables with [] after the data type.
4. The variables in the array are ordered and each has an index beginning from 0.
5. C# array is an object of base type System.Array.
6. Default values of numeric array and reference type elements are set to be respectively zero
and null.
7. A jagged array elements are reference types and are initialized to null.
8. Array elements can be of any type, including an array type.
9. Array types are reference types which are derived from the abstract base type Array. These
types implement IEnumerable and for it, they use foreach iteration on all arrays in C#.

You might also like