Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views18 pages

Computer Programming C#: Lec.08 Salem Belgurzi

This document covers the basics of arrays in C#, including how to create, access, and modify them, as well as how to loop through their elements using for and foreach loops. It also introduces two-dimensional arrays and demonstrates how to access and loop through them. Additionally, it discusses sorting arrays using the Sort() method.

Uploaded by

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

Computer Programming C#: Lec.08 Salem Belgurzi

This document covers the basics of arrays in C#, including how to create, access, and modify them, as well as how to loop through their elements using for and foreach loops. It also introduces two-dimensional arrays and demonstrates how to access and loop through them. Additionally, it discusses sorting arrays using the Sort() method.

Uploaded by

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

Computer Programming

C#
Lec.08

Salem Belgurzi
Topics
• Create an Array
• Access the Elements of an Array
• Array Length
• Other Ways to Create an Array
• Loop Through an Array
• The foreach Loop
• Two-Dimensional Arrays
Create an Array
• Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
• To declare an array, define the variable type with square brackets:
• string[] cars;
• This declares a variable that holds an array of strings.
Create an Array
• To insert values to array, place the values inside curly braces separated
by a comma :
• string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

• To create an array of integers:

• int[] myNum = {10, 20, 30, 40};


Access the Elements of an Array
• We access an array element by referring to the index
number.
• Array indexes start with 0: [0] is the first element. [1] is the
second element, etc.

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


Console.WriteLine(cars[1]);
// Outputs BMW
Change an Array Element
• To change the value of a specific element, refer to the index
number:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


cars[0] = "Opel";
Console.WriteLine(cars[0]);
// Now outputs Opel instead of Volvo
Array Length
• o find out how many elements an array has, use the Length
property:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


Console.WriteLine(cars.Length);
// Outputs 4
Other Ways to Create an Array
// Create an array of four elements, and add values later
string[] cars = new string[4];

// Create an array of four elements and add values right away


string[] cars = new string[4] {"Volvo", "BMW", "Ford", "Mazda"};

// Create an array of four elements without specifying the size


string[] cars = new string[] {"Volvo", "BMW", "Ford", "Mazda"};

// Create an array of four elements, omitting the new keyword,


// and without specifying the size
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Loop Through an Array
• You can loop through the array elements with the for loop,
and use the Length property to specify how many times the
loop should run.
string[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};

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


{
Console.WriteLine(cars[i]); }

}
The foreach Loop
• There is also a foreach loop, which is used exclusively to
loop through elements in an array:

foreach (type variableName in arrayName)


{
// code block to be executed
}
}
The foreach Loop
• The foreach method is easier to write, it does not require a
counter (using the Length property) as for loop, and it is more
readable.
• The following example outputs all elements in the cars array,
using a foreach loop:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


foreach (string i in cars)
{
Console.WriteLine(i);
} } }
Sort an Array

• There are many array methods available, for example Sort(),


which sorts an array alphabetically or in an ascending order:

// Sort a string
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Array.Sort(cars);
foreach (string i in cars)
{
Console.WriteLine(i);
}
}
Sort an Array

// Sort an int
int[] myNumbers = {5, 1, 8, 9};
Array.Sort(myNumbers);
foreach (int i in myNumbers)
{
Console.WriteLine(i);
}
Multidimensional Arrays
• Previously, we learned about arrays, which is also known as
single dimension arrays.
• A multidimensional array is basically an array of arrays.

• Arrays can have any number of dimensions. The most


common are two-dimensional arrays (2D).
Two-Dimensional Arrays
• To create a 2D array, add each array within its own set of
curly braces, and insert a comma (,) inside the square
brackets:

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };

• The single comma [,] specifies that the array is two-


dimensional. A three-dimensional array would have two
commas: int[,,].
Access Elements of a 2D Array
• To access an element of a two-dimensional array, we must
specify two indexes: one for the array, and one for the
element inside that array. Or better yet, one for the row and
one for the column.

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };


Console.WriteLine(numbers[0, 2]); // Outputs 2
Loop Through a 2D Array
• We can easily loop through the elements of a two-
dimensional array with a foreach loop:

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };


foreach (int i in numbers)
{
Console.WriteLine(i);
}

You might also like