Array
C# Programming
Arrays in C#
• Arrays are a fundamental data structure in programming.
• They allow us to store multiple values of the same type in a single variable.
• An array can be single-dimensional, multidimensional , or jagged.
• The number of dimensions and the length of each dimension are established
when the array instance is created. These values can't be changed during the
lifetime of the instance.
• The default values of numeric array elements are set to zero, and reference
elements are set to null.
• Arrays are zero-indexed: an array with n elements is indexed from 0 to n-1.
Declaring Arrays
• Arrays are declared using the following syntax:
type[ ] array-name;
• Where type is the data type of the array elements.
• The following examples show how to declare different kinds of arrays:
• Single-dimensional arrays:
int[] numbers;
• Multidimensional arrays:
int[,] matrix;
• Array-of-arrays (jagged):
byte[][] scores;
Declaring Arrays cont…
• In C#, arrays are objects. That means that declaring an array
doesn't create an array. After declaring an array, you need to
instantiate an array by using the "new" operator.
• The following syntax defines arrays of different data types
double[] doubleArray = new double[5];
char[] charArray = new char[5];
bool[] boolArray = new bool[2];
string[] stringArray = new string[10];
Initializing Arrays
• Once an array is declared, the next step is to initialize the array. The initialization
process of an array includes adding actual data to the array.
• The following sample syntaxes creates an array of 3 items and the values of these
items are added when the array is initialized.
int[] sArray = new int[3] {1, 3, 5};
• Alternative, we can also add array items one at a time as listed in the following code
snippet.
int[ ] sArray = new int[3];
sArray[0] = 1;
sArray[1] = 3;
sArray[2] = 5;
string[] names = new string[3] {"Matt", "Joanne", "Robert"};
Accessing array elements
• We can access an array item by passing the item index in the array.
• Access array elements using subscripts or indexes:
value = arrayName[index];
• The following syntax creates an array of three items and displays those items on the
console.
int[] staticIntArray = new int[3];
staticIntArray[0] = 1;
staticIntArray[1] = 3;
staticIntArray[2] = 5;
Console.WriteLine(staticIntArray[0]);
Console.WriteLine(staticIntArray[1]);
Console.WriteLine(staticIntArray[2]);
• We can also use loop to access data from an array.
string[ ] strArray = new string[]{ “Bhuwan”, “Sabin”, “Ajin”, “Ram", “Harry”};
foreach (string str in strArray)
{
Console.WriteLine(str);
}
Retrieving Array
Metadata
• The Array class provides several properties
for retrieving metadata about an array
• Length: Returns the total number of
elements in all dimensions of an array.
• GetLength: Returns the number of
elements in the specified dimension of an
array
• Rank: Returns the number of dimensions
of an array
• GetType: Returns the Type of the current
array instance
Example
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine("Integer Array:");
Console.WriteLine("Length: " +
numbers.Length);
string[] names = { "John", "Jane",
"Alice" };
Console.WriteLine("String Array:");
Console.WriteLine("Length: " +
names.Length);
Array Methods
• C# provides methods like Sort()
and Reverse() for sorting and
reversing arrays.
• Sort() arranges the elements in
ascending order.
• Reverse() reverses the order of
elements.
Example
int[] numbers = { 5, 2, 8, 1, 4 };
// Sorting the array in ascending order
Array.Sort(numbers);
Console.WriteLine("Sorted Array (Ascending Order):");
foreach (int num in numbers)
{
Console.Write(num + " ");
}
Console.WriteLine();
// Reversing the array
Array.Reverse(numbers);
Console.WriteLine("Reversed Array:");
foreach (int num in numbers)
{
Console.Write(num + " ");
}
Console.WriteLine();
Summary
• Today, we covered important concepts related to the C# programming
language.
• We learned about declaring and assigning values to arrays, accessing
elements using subscripts, and using the Length property.
• We also explored the Sort() and Reverse() methods for array
manipulation.
• Additionally, we discussed other useful array methods.
Thank You