Web Design and Development

Web Design and Development

Topic: Arrays and Collections in C#

Exercise 1 – Answer the following questions:

• Why would you use an array instead of creating multiple variables with various names?

Variables can only hold one value at a time, so you would use an array to hold multiple variables. Arrays also prevents the issue of previously initialized variable values from being deleted by set statements later in the program.

• How do you add elements to an array?

Using a new array larger than the original to add a new element.

Using Array List as an intermediate structure.

Shifting the elements to accommodate the new element.

• How would you check if an element’s value exists in the array?

By using the includes () method which returns true if an array contains a specified element, otherwise false

• Why would you use a List instead of an array in C#?

A List uses an internal array to handle its data, and automatically resizes the array when adding more elements to the List than its current capacity, which makes it easier to use than an array, where you need to know the capacity beforehand.

Exercise 2: Storing Elements in array

Write a program in C# to create an array of five subjects. Access the subjects by referring to the index number.

1. Start your visual studio 2019

2. Click on Create a new project

3. Search for console in search box and select “Console App (.Net Core)

4. Name you project “Tutorial3Ex2” and click on Create

5. Inside the main function add the code below

Text  Description automatically generated

6. Click on start to run the application and see the output

What to submit:

a. The source code of Program file (Main function)

using System;

namespace tutorial_3Ex1

{

class Program

{

static void Main(string[] args)

{

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

Console.WriteLine(cars[0]);

Console.WriteLine(cars[1]);

Console.WriteLine(cars[2]);

Console.WriteLine(cars[3]);

}

}

}

b. The output of the program

Text

Description automatically generated
 (Less important)

Exercise 3: Array Methods

Use Min(), Max() and Sum() methods on an array of integer and observe the results:

1. Start your visual studio 2019

2. Click on Create a new project

Graphical user interface, application

Description automatically generated
 (Less important)

3. Search for console in search box and select “Console App (.Net Core)

Graphical user interface, text, application, email

Description automatically generated

4. Name you project “Tutorial3Ex3” and click on Create

5. Inside the main function add the code below

6. Click on start to run the application and see the output

What to submit:

a. The source code of Program file (Main function)

using System;

using System.Linq;

namespace tutorial_3Ex2

{

class Program

{

static void Main(string[] args)

{

int[] myNumbers = { 5, 1, 8, 9 };

Console.WriteLine(myNumbers.Max());

Console.WriteLine(myNumbers.Min());

Console.WriteLine(myNumbers.Sum());

}

}

}

b. The output of the program

Text

Description automatically generated
 (Less important)

Exercise 4: Create a list of integers (11,34, 56, 78, 1, 55) and name it as firstlist. Display/print the total number of elements (count) of firstlist. Add some more elements (41, 22, 90, 21) in firstlist and display the total number of elements of list. Check if 57 is present in the list or not?

1. Start your visual studio 2019

2. Click on Create a new project

Graphical user interface, application

Description automatically generated
 (Less important)
Graphical user interface, text, application, email

Description automatically generated

3. Search for console in search box and select “Console App (.Net Core)

4. Name you project “Tutorial3Ex4” and click on Create

5. Inside the main function add the code below

6. Run the application and observe the output.

What to submit:

a. The source code of Program file (Main function)

using System;

using System.Linq;

using System.Collections.Generic;

namespace tutorial_3

{

class Program

{

static void Main(string[] args)

{

List<int> firstlist = new List<int>();

firstlist.Add(11);

firstlist.Add(34);

firstlist.Add(56);

firstlist.Add(78);

firstlist.Add(1);

firstlist.Add(55);

Console.WriteLine(“1st Count is: ” + firstlist.Count);

firstlist.Add(41);

firstlist.Add(22);

firstlist.Add(90);

firstlist.Add(21);

Console.WriteLine(“2nd Count is: ” + firstlist.Count);

Console.WriteLine(“Total number of element of list: ” + firstlist.Count);

Console.Write(firstlist.Contains(57));

}

}

}

b. The output of the program

Text

Description automatically generated
 (Less important)

Leave a Comment