c# - Foreach Loop Is Not Executing -
there similar type of solution found , tried follow-up still failed solve.
i have created student class public property first_name & last_name. using arraylist wanted & print details of each student. after executing code got empty console window. when run code using breakpoint saw flow of code not entering foreach loop. please me....
arraylist studentlist = new arraylist(); public void getstudent() { foreach (student student in studentlist) { console.write("enter first name: "); student.first_name = console.readline(); console.write("enter last name: "); student.last_name = console.readline(); } } public void printstudentinformation() { foreach (student student in studentlist) { console.writeline("-----------------------"); console.writeline("name of student {0} {1}", student.first_name, student.last_name); console.writeline("-----------------------"); } }
when run code first time there no student in studentlist
, foreach
in getstudent
won't executed. create new instance of student
class store firstname
, lastname
in student object , add student object studentlist
array.
public void getstudent() { var student = new student(); console.write("enter first name: "); student.first_name = console.readline(); console.write("enter last name: "); student.last_name = console.readline(); studentlist.add(student); }
Comments
Post a Comment