Trying to display the names and student ID with a foreach loop at the end of the program, but instead of listing the names it just gives me the output "Assignment_8.Student". How exactly would I display the names and ID's instead of that directory its giving me?
using System;
using System.Collections.Generic;
namespace Assignment_8
{
class Program
{
static void Main(string[] args)
{
List<Student> myList = new List<Student>();
Student s1 = new Student();
s1.FirstName = "John";
s1.LastName = "Smith";
s1.StudentID = 2560;
myList.Add(s1);
Student s2 = new Student("Peter");
myList.Add(s2);
Student s3 = new Student("Morgan", "Simmons");
myList.Add(s3);
Student s4 = new Student("James", "Walters");
myList.Add(s4);
Student s5 = new Student("Linda", "Scott", 1005);
myList.Add(s5);
Console.WriteLine();
Console.WriteLine("Total students: {0}", Student.Count);
foreach (var item in myList)
{
Console.WriteLine("Student: {0}", item);
}
}
}
}
Here is the Class:
using System;
using System.Collections.Generic;
using System.Text;
namespace Assignment_8
{
class Student
{
public static int Count { get; private set; }
private static readonly Random rnd = new Random();
public string FirstName { get; set; }
public string LastName { get; set; }
public int StudentID { get; set; }
public Student(string first, string last, int id)
{
FirstName = first;
LastName = last;
StudentID = id;
++Count;
Console.WriteLine("Student Name: {0} {1}, Student ID: {2}",
FirstName, LastName, StudentID );
}
public Student(string first ="", string last ="")
{
FirstName = first;
LastName = last;
++Count;
Console.WriteLine("Student Name: {0} {1}, Student ID: {2}",
FirstName, LastName, rnd.Next(1000, 9999) );
}
}
}
Here is the Output of the program:
Student Name: , Student ID: 4101
Student Name: Peter , Student ID: 9074
Student Name: Morgan Simmons, Student ID: 2328
Student Name: James Walters, Student ID: 5481
Student Name: Linda Scott, Student ID: 1005
Total students: 5
Student: Assignment_8.Student
Student: Assignment_8.Student
Student: Assignment_8.Student
Student: Assignment_8.Student
Student: Assignment_8.Student