In: Computer Science
Create a C# .NET Core Console project in Visual Studio. (This is the same kind of project we have been doing all semester.) Do all of the following in the Program class. You do not need to add any other classes to this project. 2. If it exists, remove the Console.WriteLine(“Hello World!”); line that Visual Studio created in the Program class. 3. At the very top of the Program.cs page you should see using System; On the empty line below that add using System.Collections.Generic; This tells C# to include the generic collection namespace which contains the generic collection classes. 4. Create a private field named daysList which is a List containing the days of the week: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday 5. Do the same thing as #2 for fields named daysDictionary, daysStack, and daysQueue which are Dictionary, Stack, and Queue respectively. For the dictionary use 0,1,2,3,4,5,6 as the keys. 6. Finally, use a foreach loop to iterate through one of the collections (you choose whichever one you want) and display that collection’s values to the screen.
The C# program is written as per the requirement. It is
self-explanatory. As described in the requirements, there are
collections declared, one each for storing the days of the week in
List, Dictionary, Stack and Queue. The values are initialised
within the main program.
A foreach loop is created to loop through the List and display the
values on the screen.
Code
using System;
using System.Collections.Generic;
namespace Test
{
class Program
{
private static List<DayOfWeek> daysList = new
List<DayOfWeek>(7);
private static Dictionary<int, DayOfWeek> daysDictionary =
new Dictionary<int, DayOfWeek>(7);
private static Stack<DayOfWeek> daysStack = new
Stack<DayOfWeek>(7);
private static Queue<DayOfWeek> daysQueue = new
Queue<DayOfWeek>(7);
static void Main(string[] args)
{
//Add elements to the List
daysList.Add(DayOfWeek.Sunday);
daysList.Add(DayOfWeek.Monday);
daysList.Add(DayOfWeek.Tuesday);
daysList.Add(DayOfWeek.Wednesday);
daysList.Add(DayOfWeek.Thursday);
daysList.Add(DayOfWeek.Friday);
daysList.Add(DayOfWeek.Saturday);
//Add elements to the Dictionary
daysDictionary.Add(0, DayOfWeek.Sunday);
daysDictionary.Add(1, DayOfWeek.Monday);
daysDictionary.Add(2, DayOfWeek.Tuesday);
daysDictionary.Add(3, DayOfWeek.Wednesday);
daysDictionary.Add(4, DayOfWeek.Thursday);
daysDictionary.Add(5, DayOfWeek.Friday);
daysDictionary.Add(6, DayOfWeek.Saturday);
//Add elements to the stack
daysStack.Push(DayOfWeek.Sunday);
daysStack.Push(DayOfWeek.Monday);
daysStack.Push(DayOfWeek.Tuesday);
daysStack.Push(DayOfWeek.Wednesday);
daysStack.Push(DayOfWeek.Thursday);
daysStack.Push(DayOfWeek.Friday);
daysStack.Push(DayOfWeek.Saturday);
//Add elements to the Queue
daysQueue.Enqueue(DayOfWeek.Sunday);
daysQueue.Enqueue(DayOfWeek.Monday);
daysQueue.Enqueue(DayOfWeek.Tuesday);
daysQueue.Enqueue(DayOfWeek.Wednesday);
daysQueue.Enqueue(DayOfWeek.Thursday);
daysQueue.Enqueue(DayOfWeek.Friday);
daysQueue.Enqueue(DayOfWeek.Saturday);
//Loop through the Days List and print the values to the
screen.
foreach (DayOfWeek day in daysList)
{
Console.WriteLine(day);
}
Console.Write("\nPress any key to continue");
Console.ReadKey();
}
}
}