C#. Lecture No. 4
C#. Lecture No. 4
Lecture 4
3rd Class
Prepared by Dr. Yousif A. Hamad
Contents
C# Arrays- Create an Array ........................................................................................................................ 2
Array Length ............................................................................................................................................ 3
Loop Through an Array .......................................................................................................................... 4
C# Sort Arrays ............................................................................................................................................. 5
System.Linq Namespace ........................................................................................................................ 6
C# Multidimensional Arrays ...................................................................................................................... 7
Loop Through a 2D Array ...................................................................................................................... 8
C# Arrays- Create an Array
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for
each value.
string[] cars;
To insert values to it, we can use an array literal - place the values in a comma-separated list, inside
curly braces:
Console.WriteLine(cars[0]);
// Outputs Volvo
Note: Array indexes start with 0: [0] is the first element. [1] is the second element,
Example
cars[0] = "Opel";
Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
Console.WriteLine(cars[0]);
Array Length
To find out how many elements an array has, use the Length property:
Console.WriteLine(cars.Length);
// Outputs 4
It is up to you which option you choose. In our tutorial, we will often use the last option, as it is faster
and easier to read.
However, you should note that if you declare an array and initialize it later, you have to use
the new keyword:
// Declare an array
string[] cars;
Console.WriteLine(cars[i]);
Syntax
foreach (type variableName in arrayName)
The following example outputs all elements in the cars array, using
a foreach loop:
Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars)
{
Console.WriteLine(i);
}
Output
Volvo
BMW
Ford
Mazda
The example above can be read like this: for each string element (called i - as
in index) in cars, print out the value of i.
If you compare the for loop and foreach loop, you will see that
the foreach method is easier to write, it does not require a counter (using
the Length property), and it is more readable.
C# Sort Arrays
There are many array methods available, for example Sort(), which sorts an array
alphabetically or in an ascending order:
// Sort a string
Array.Sort(cars);
Console.WriteLine(i);
}
// Sort an int
Array.Sort(myNumbers);
Console.WriteLine(i);
System.Linq Namespace
Other useful array methods, such as Min, Max, and Sum, can be found in
the System.Linq namespace:
Example
using System;
using System.Linq;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
}
C# Multidimensional Arrays
In the previous chapter, you learned about arrays, which is also known as single
dimension arrays. These are great, and something you will use a lot while
programming in C#. However, if you want to store data as a tabular form, like a
table with rows and columns, you need to get familiar with multidimensional
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:
numbers is now an array with two arrays as its elements. The first array element
contains three elements: 1, 4 and 2, while the second array element contains 3,
6 and 8. To visualize it, think of the array as a table with rows and columns:
Example
int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
Remember that: Array indexes start with 0: [0] is the first element. [1] is the
second element, etc.
The following example will change the value of the element in the first row
(0) and first column (0):
Example
int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
Example
int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
Console.WriteLine(i);
}
You can also use a for loop. For multidimensional arrays, you need one loop for
each of the array's dimensions.
Also note that we have to use GetLength() instead of Length to specify how many
times the loop should run:
Example
int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
Example
using System;
using System.Linq;
namespace CarDemo
{
class Program
{
static void Main(string[] args)
{
int[] std = { 55, 60, 77, 82, 47, 60 };
Array.Reverse(std);
Console.WriteLine("\n============================");
foreach (int i in std)
Console.Write(i + " ");
}
}
}
Example
using System;
using System.Linq;
namespace CarDemo
{
class Program
{
static void Main(string[] args)
{
// array declarations
int[] st = { 2, 4, 6, 12, 15, 22, 40 };
Array.Reverse(st);
}
}
}
Q/ Write a C# program of the following requirements (2D Array of 3 x
4) type int
1- Print the sum of (2,2), (0,3)
2- Print the average of array
3- Print the sum of array
4- Change the elements of 1st row 2nd column
5- Print the length of array
6- Print the last row of array
7- Print the 2nd column of array
8- Print the sorted array
9- Print the reverse array
10- Print the sum of elements ((2,3), (1,0),( 2,2))
Q/ Write the output of the following C# Program
using System;
using System.Linq;
namespace CarDemo
{
class Program
{
static void Main(string[] args)
{
int[,] st = { { 2, 3 ,7,8}, { 4, 9,12,22 }, { 5, 11, 40, 55 } };
int s = 0;
Console.WriteLine("\n************************");
for (int i = 0; i < 3; i++)
Console.WriteLine("\n************************");
} } }
Example // 2D array of 3 x 4 type int
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{// 2D array of 3 x 4 type int
int[,] ary = { {1,2,3,9 }, {2,4,6,10 }, {3,5,7,12 } };
int s = ary[2, 2] + ary[0, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
Console.Write(ary[i, j] + " ");
Console.WriteLine();
}
Console.WriteLine("\n ---------------");
Console.WriteLine("The sum of ary[2, 2] + ary[0, 3]= " +
s);
Console.WriteLine("\n ---------------");
int s1 = 0;
foreach (int i in ary)
s1 += i;
Console.WriteLine("The sum of ary= " + s1);
Console.WriteLine("\n ---------------");
Console.WriteLine("The AVG of ary= " + (s1/12));
Console.WriteLine("\n ---------------");
ary[0, 1] = 40;
Console.WriteLine("The length of ary= " +ary.Length);
Console.WriteLine("\n ---------------");
for (int j = 0; j < 4; j++)
Console.Write(ary[1,j]+ " ");
Console.WriteLine("\n ---------------");
for (int j = 0; j < 3; j++)
Console.WriteLine(ary[j, 3] );
}
}
}
Example using Array of string with 4 elements
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
string [] cars = { "Volvo", "BMW", "Ford", "Mazda" };
//Print the elements of an array
for (int i = 0; i < cars.Length; i++)
Console.Write(cars[i] + " , ");
Console.WriteLine("\n-------------------------------");
//Print the last element of array
Console.Write("The last element is : "+cars[3]);
Console.WriteLine("\n-------------------------------");
//Change the 2nd elements to Opel
cars[1] = "Opel";
//Print the elements of an array using Foreach
foreach (string i in cars)
Console.Write(i + " , ");
Console.WriteLine("\n-------------------------------");
// Sort and print the array
Array.Sort(cars);
for (int i = 0; i < cars.Length; i++)
Console.Write(cars[i] + " , ");
Console.WriteLine("\n-------------------------------");
//Print the length of the array
Console.Write("The Array length is : " + cars.Length);
Console.WriteLine("\n-------------------------------");
//Print the Reverse of the array
Array.Reverse(cars);
for (int i = 0; i < cars.Length; i++)
Console.Write(cars[i] + " , ");
}
}
}