C# Array PDF
C# Array PDF
Create an Array in C#
Syntax
data_type [] name_of_array
Declaration of an Array:
class Name
{
static void Main(string[]args)
{
Int32[] intarray; //array declaration
}
}
Array Initialization:
class Name
{
static void Main(string[]args)
{
Int32[] Intarray; //array declaration
Intarray = new Int32[4]; // array initialization
Intarray[0]= 23; // assigning values to the elements
Intarray[1]=5;
Intarray[2]=88;
Intarray[3]=6;
}
}
Displaying values of Array:
class Name
{
static void Main(string[]args)
{
Int32[] Intarray; //array declaration
Intarray = new Int32[4]; //array initialization
Intarray[0]= 23; //assigning values to array
Intarray[1]=5;
Intarray[2]=88;
Intarray[3]=6;
Console.WriteLine(Intarray[0]);
Console.WriteLine(Intarray[1]);
Console.WriteLine(Intarray[2]);
Console.WriteLine(Intarray[3]);
Console.ReadKey();
}
}
Examples:
using System;
namespace ArrayDemo
{
class Name
{
static void Main(string[] args)
{
Int32[] Intarray; // array declaration
Intarray = new Int32[4]; // array initialization
Intarray[0] = 23; // assigning values to the array element
Intarray[1] = 5;
Intarray[2] = 88;
Intarray[3] = 6;
Console.WriteLine(Intarray[0]);
Console.WriteLine(Intarray[1]);
Console.WriteLine(Intarray[2]);
Console.WriteLine(Intarray[3]);
Console.ReadKey();
}
}
}
Example - 2:
using System;
namespace Demo
{
class Array
{
static void Main(string[] args)
{
int[] arr = new int[4] { 10, 20, 30, 40 };
for (int i = 0; i < arr.Length; i++) // Traverse array elements
{
Console.WriteLine(arr[i]);
}
}
}
}
Example - 3:
using System;
namespace Demo
{
class Array
{
static void Main(string[] args)
{
int[] arr = new int[4] { 10, 20, 30, 40 };
foreach (int i in arr)
{
Console.WriteLine(i);
}
}
}
}
2. Multi-dimension array.
3. Jagged array.
Multi-dimension Array
Multi-dimensional arrays, data is stored in tabular form. In a multidimensional array, each element of the array is
also an array.
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
Here, x is a multidimensional array which has two elements: {1, 2, 3} and {3, 4, 5}. And, each element of
the array is also an array with 3 elements.
Two-dimensional array in C#
A two-dimensional array consists of single-dimensional arrays as its elements. It can be represented as a
table with a specific number of rows and columns.
Here, x is a two-dimensional array with 2 elements. And, each element is also an array with 3 elements.
So, all together the array can store 6 elements (2 * 3).
Note: The single comma [ , ] represents the array is 2 dimensional.
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
Here, x is a 2D array with two elements {1, 2, 3} and {3, 4, 5} . We can see that each element of the
array is also an array.
We can also specify the number of rows and columns during the initialization
// a 2D array
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
Example:
using System;
namespace MultiDArray {
class Program {
static void Main(string[] args) {
//initializing 2D array
int[ , ] numbers = {{2, 3}, {4, 5}};
// access first element from the first row
Console.WriteLine("Element at index [0, 0] : "+numbers[0, 0]);
// access first element from second row
Console.WriteLine("Element at index [1, 0] : "+numbers[1, 0]);
}
}
}
using System;
namespace MultiDArray {
class Program {
static void Main(string[] args) {
int[ , ] numbers = {{2, 3}, {4, 5}};
// old element
Console.WriteLine("Old element at index [0, 0] : "+numbers[0, 0]);
// assigning new value
numbers[0, 0] = 222;
// new element
Console.WriteLine("New element at index [0, 0] : "+numbers[0, 0]);
}
}
}
using System;
namespace MultiDArray {
class Program {
static void Main(string[] args) {
int[ , ] numbers = { {2, 3, 9}, {4, 5, 9} };
for(int i = 0; i < numbers.GetLength(0); i++) {
Console.Write("Row "+ i+": ");
for(int j = 0; j < numbers.GetLength(1); j++) {
Console.Write(numbers[i, j]+" ");
}
Console.WriteLine();
}
}
}
}Note:
namespace Addmatrices
{
class Program
{
static void Main(string[] args)
{
int i, j, n;
int[,] arr1 = new int[50, 50];
int[,] brr1 = new int[50, 50];
int[,] crr1 = new int[50, 50];
dimensions.
Here's an example of a jagged array in C# that stores the grades of a classroom of students:
In this example, we've created a jagged array grades with three sub-arrays. The first sub-array
contains four grades, the second sub-array contains three grades, and the third sub-array contains
five grades. Notice that we didn't have to specify the length of each sub-array when we declared the
grades array; instead, we created each sub-array separately and assigned it to the appropriate
To access an element in a jagged array, you need to use two sets of square brackets. The first set of
brackets specifies the index of the sub-array you want to access, and the second set of brackets
specifies the index of the element within that sub-array.
For example, to access the grade 76 in the first sub-array of the grades array, you would use the
following code:
int x = grades[0][1];
Jagged arrays can be useful in a variety of situations, such as storing data from a database query or
representing a grid of values where each row may have a different number of columns. They also have
the advantage of being more memory-efficient than multidimensional arrays, since they only allocate
memory for the sub-arrays that are actually needed. However, accessing elements in a jagged array
can be slightly slower than accessing elements in a multidimensional array, since it requires an extra
level of indirection.
In C#, a string array is an array of strings, where each element of the array is a string. Here's an
In this example, we've created a string array colors with four elements, each of which is a string that
represents a color. Notice that we used the new keyword to allocate memory for the array and
You can access an element in a string array by using its index, which is an integer value that
represents its position in the array. For example, to access the third element of the colors array (which
You can also change the value of an element in a string array by assigning a new string value to it. For
example, to change the value of the second element of the colors array to "purple", you would use
the following code:
colors[1] = "purple";
C# Multidimensional Arrays
A multidimensional array in C# is an array of arrays, where each element of the array is itself an array.
A two-dimensional array, for example, is an array of rows, where each row is an array of columns.
Here's an example of a two-dimensional array in C# that stores the grades of a classroom of students:
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
In this example, we've created a two-dimensional array matrix with 3 rows and 4 columns. Each row
represents a set of numbers and each column represents a specific value. The values of the array have
been initialized to the numbers 1 through 12.
We can access specific elements of the array using their indices like this:
int value = matrix[0, 2]; // This gets the value in row 0, column 2 (which is 3)
We can also modify elements of the array by assigning new values to them:
We can even use loops to iterate through the elements of the array and perform operations on them:
Console.WriteLine();
This code uses a nested for loop to iterate through each element of the array and print its value to the
console. The GetLength method is used to get the length of each dimension of the array, which allows
us to iterate through all the elements even if we don't know the exact size of the array.