For the following code. I have to hit the enter button to make the rest of the m
ID: 3851828 • Letter: F
Question
For the following code. I have to hit the enter button to make the rest of the material show. I want it to display all the material without me having to hit enter, What am i doing wong? The code and output is below. Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lynch_Jessica_Final_Exam
{
class Program
{
static void Main(string[] args)
{
Course course1 = new Course
{
ID = "CTS1851",
Name = "Internet Web Foundation"
};
Course course2 = new Course
{
ID = "CGS2820",
Name = "Web Programming"
};
Course course3 = new Course
{
ID = "CGS2821",
Name = "Advanced Web Programming"
};
Course course4 = new Course
{
ID = "COP2361",
Name = "C# Programming"
};
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add(course1.ID, course1.Name);
dictionary.Add(course2.ID, course2.Name);
dictionary.Add(course3.ID, course3.Name);
dictionary.Add(course4.ID, course4.Name);
foreach (KeyValuePair<string, string> kvp in dictionary)
{
Console.WriteLine("Course ID: {0}, Course Name: {1}", kvp.Key, kvp.Value);
Console.ReadLine();
}
}
}
}
namespace Lynch_Jessica_Final_Exam
{
public class Course
{
string courseName;
string courseId;
public Course()
{ }
public Course(string CID, string CName)
{
courseName = CName;
courseId = CID;
}
public string ID
{
get { return courseId; }
set { courseId = value; }
}
public string Name
{
get { return courseName; }
set { courseName = value; }
}
}
}
Output:
file:///c/users/miracle-pc/documents/visual studio 20·.. Course ID: CTS1851, Course Name: Internet Web Foundation Course ID: CGS2820, Course Name: Web Programming Course ID: CGS2821, Course Name: Advanced Web Programming Course ID: COP23 C# Programming 61, Course Name:Explanation / Answer
CORRECTED C# CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lynch_Jessica_Final_Exam
{
class Program
{
static void Main(string[] args)
{
Course course1 = new Course
{
ID = "CTS1851",
Name = "Internet Web Foundation"
};
Course course2 = new Course
{
ID = "CGS2820",
Name = "Web Programming"
};
Course course3 = new Course
{
ID = "CGS2821",
Name = "Advanced Web Programming"
};
Course course4 = new Course
{
ID = "COP2361",
Name = "C# Programming"
};
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add(course1.ID, course1.Name);
dictionary.Add(course2.ID, course2.Name);
dictionary.Add(course3.ID, course3.Name);
dictionary.Add(course4.ID, course4.Name);
foreach (KeyValuePair<string, string> kvp in dictionary)
{
Console.WriteLine("Course ID: {0}, Course Name: {1}", kvp.Key, kvp.Value);
//Console.ReadLine(); // you were doing the mistake here,
}
Console.ReadLine(); // put it here
}
}
}
namespace Lynch_Jessica_Final_Exam
{
public class Course
{
string courseName;
string courseId;
public Course()
{ }
public Course(string CID, string CName)
{
courseName = CName;
courseId = CID;
}
public string ID
{
get { return courseId; }
set { courseId = value; }
}
public string Name
{
get { return courseName; }
set { courseName = value; }
}
}
}
Explanation of the error: You were doing a funny mistake, you had put Console.ReadLine(); in the body of foreach loop of main method, hence each time the console was waiting to be pressed a key by you to continue displaying the information. So the correction is that you need to put Console.ReadLine(); statement outside the foreach loop and inside the main method. Thats all. :-)
CORRECTED C# CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lynch_Jessica_Final_Exam
{
class Program
{
static void Main(string[] args)
{
Course course1 = new Course
{
ID = "CTS1851",
Name = "Internet Web Foundation"
};
Course course2 = new Course
{
ID = "CGS2820",
Name = "Web Programming"
};
Course course3 = new Course
{
ID = "CGS2821",
Name = "Advanced Web Programming"
};
Course course4 = new Course
{
ID = "COP2361",
Name = "C# Programming"
};
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add(course1.ID, course1.Name);
dictionary.Add(course2.ID, course2.Name);
dictionary.Add(course3.ID, course3.Name);
dictionary.Add(course4.ID, course4.Name);
foreach (KeyValuePair<string, string> kvp in dictionary)
{
Console.WriteLine("Course ID: {0}, Course Name: {1}", kvp.Key, kvp.Value);
//Console.ReadLine(); // you were doing the mistake here,
}
Console.ReadLine(); // put it here
}
}
}
namespace Lynch_Jessica_Final_Exam
{
public class Course
{
string courseName;
string courseId;
public Course()
{ }
public Course(string CID, string CName)
{
courseName = CName;
courseId = CID;
}
public string ID
{
get { return courseId; }
set { courseId = value; }
}
public string Name
{
get { return courseName; }
set { courseName = value; }
}
}
}