What is array
An array is a type that holds multiple variables of one type, allowing index access to the individual value.
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
An array in .NET is created from Array base class. Array is a reference type because it is created from array class. The base class for arrays in C# is the System.Array class.
Array is fixed type. You can create array of primitive and non-primitive types. Array base address is stored in stack and array is kept in heap memory.
System.Array is the abstract base type of all array types. Array type can't be inherited. Array can't be extended. There size is fixed. Private constructor and inner class is use to create it.
Type of Arrays in C#.NET
- Single-dimensional arrays
- Multidimensional arrays (rectangular arrays)
- Jagged Arrays (array-of-arrays)
- Mixed Arrays
Single Dimensional Array
Single-dimensional arrays are the simplest form of arrays. These types of arrays are used to store number of items of a predefined type. All items in a single dimension array are stored in a row starting from 0 to the size of array - 1.
Single Dimensional array is also called one-dimensional array.
DataType[] VariableName = new DataType[Number];
In the above declaration DataType can be char, int, float, double, decimal, string, etc.
VariableName is the name of the array.
The square brackets [] on the left of the assignment operator are used to let the compiler know that you are declaring an array instead of a normal variable.
The new operator allows the compiler to reserve memory.
The [Number] factor is used to specify the number of items of the list.
Declaring single dimensional array
int[] arr = new int[10];
double numbers = new double[5];
string[] names = new string[2];
Single dimensional array initialization
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6,7,8,9,10 };
string[] names = new string[] { "Rocky", "Sam", "Tina", "Yoo", "James", "Samantha" };
You can omit the size of the array, like this:
int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Reena", "Prety", "Rocky", "David");
You can also omit the new operator if you initialize array like this:
int[] numbers = {1, 2, 3, 4, 5};
string[] names = {"Reena", "Prety", "Rocky", "David"};
Practical demonstration of single dimensional array
using System;
namespace array_example1
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10];
for (int x = 0; x < 10; x++)
{
Console.WriteLine("Enter array element : {0}", x + 1);
arr[x] = Int32.Parse(Console.ReadLine());
}
foreach (int i in arr)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
Note: If you do not initialize an array at the time of declaration, the array members are automatically initialized to the default initial value for the array type. Also, if you declare the array as a field of a type, it will be set to the default value null when you instantiate the type.
Something about foreach
- In a for loop, you should know the number of element of the array. If you don't, the C# language allows you to let the compiler use its internal mechanism to get this count and use it to know where to stop counting. To assist you with this, C# provides the foreach operator.
- foreach loop is used on enumerator type only.
- foreach is read-only loop.
Multidimensional arrays (rectangular arrays)
Arrays can have more than one dimension; these arrays-of-arrays are called multidimensional arrays. They are very similar to standard arrays with the exception that they have multiple sets of square brackets after the array identifier.
The most common kind of multidimensional array is the two-dimensional array. A two dimensional array can be thought of as a grid of rows and columns.
Declaring multidimensional array
string[,] names = new string[4, 4];
int[,] numbers = new int[3, 4];
Multidimensional array initialization
int[,] numbers = new int[3, 2] {{1,2}, {3,4}, {5,6}};
string[,] names = new string[2, 2] {{"Ana", "Anita"}, {"Bob", "Barbie"}};
You can omit the size of the array, like this:
int[,] numbers = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
string[,] names = new string[,] {{"Ana", "Anita"}, {"Bob", "Barbie"}};
You can also omit the new operator if you initialize array like this:
int[,] numbers = {{1,2}, {3,4}, {5,6}};
string[,] names = {{"Ana", "Anita"}, {"Bob", "Barbie"}};
Practical demonstration of multidimensional array
using System;
namespace array_multidim
{
class Program
{
static void Main(string[] args)
{
//declaring a array with 4 rows and 3 columns
int[,] arr = new int[4, 3];
//accepting values in the array
for (int x = 0; x < 4; x++)
{
Console.WriteLine("Enter row element : {0}", x + 1);
for (int y = 0; y < 3; y++)
{
arr[x, y] = Int32.Parse(Console.ReadLine());
}
}
// displaying values of multidimensional array
for (int x = 0; x < 4; x++)
{
Console.WriteLine("Values of row : {0}", x + 1);
for (int y = 0; y < 3; y++)
{
Console.Write(arr[x, y] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Jagged Arrays (array-of-arrays)
Jagged arrays are often called array of arrays. More specifically, a jagged array is an array of vectors. Unlike rectangular arrays, a jagged array, as the name implies, is jagged as each vector in the array can be of different length.
It is also possible to have jagged arrays, whereby "rows" may be different sizes. For this, you need an array in which each element is another array. However, all this is only possible if the arrays have the same base type.
Declaring and initializing jagged array.
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[3];
jaggedArray[1] = new int[5];
jaggedArray[0] = new int[] { 3, 5, 7, };
jaggedArray[1] = new int[] { 1, 0, 2, 4, 6 };
You can also initialize the array upon declaration like this:
int[][] jaggedArray = new int[][]
{
new int[] { 3, 5, 7, },
new int[] { 1, 0, 2, 4, 6 }
};
You can use the following shorthand form:
int[][] jaggedArray =
{
new int[] { 3, 5, 7, },
new int[] { 1, 0, 2, 4, 6 }
};
Gurpreet AroraPosted Jul 23, 2021, 8:39 AM
Nice article
Aryan KumarPosted Jul 22, 2021, 5:21 PM
Good work !!