Accessing element of a List. C# -
i have created list of type students. following classes ones use;
public class studentdetails { public class address { public int housenumber{ get; set; } public string lineone{ get; set; } public string linetwo{ get; set; } } public class student { public int studentid { get; set; } public address studentaddress{ get; set; } } public list<student> getstudents() { private address studentone = new address{//details}; private address studenttwo = new address{//details}; var students = new list<student>(); students.add(new student {studentid = 1, studentadress = studentone, //details}); //more students return students; } }
now access details of particular student object. want house number of student. how can that? attempted create list, add list returned getstudents()
. when iterate through , references objects.
//to access list studentdetails student = new studentdetails(); //create new instance (int = 0; < student.getstudents().count; i++) { //console.writeline(student[1].getstudents()); }
you can use linq select student searching , access properties:
var student = getstudents().firstordefault(student => student.studentid /* student id here */>); if (student != null) { var housenumber = student.address.housenumber; }
Comments
Post a Comment