Accessing element of a List. C#

I have created a List of type Students. The following classes are the ones I 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 I would like to access certain details of a particular student from this object. Say I want to get the House number of a student. How can i do that? I attempted to create another list, then add the list returned from GetStudents(). However when i iterate through it , i only get references to objects.

//To access the List 

  StudentDetails student = new StudentDetails(); //create new instance
  for (int i = 0; i < student.GetStudents().Count; i++)

You can use Linq to select the student you are searching for and then access its properties:

var student = GetStudents().FirstOrDefault(student => student.StudentId /* your student id here */>);
if (student != null)
    var houseNumber = student.Address.HouseNumber;

List<T>.Item[Int32] Property, Starting with C# 8.0 you can use Index and Range classes for accessing elements. They provides accessing from the end of sequence or just� ' Shows how to access the list using the Item property. Console.WriteLine(vbLf & "dinosaurs(3): {0}", dinosaurs(3)) Remarks. List<T> accepts null as a valid value for reference types and allows duplicate elements. This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[index].

Try this

StudentDetails student = new StudentDetails(); //create new instance
foreach (var s in student.GetStudents())
   var id = s.StudentId;//use any properties using . operator

Getting a list item by index, The existing elements will be copied to the new array before the addition of the new element. Syntax: public T this[int index] { get; set; }. Parameter� C# List C# List is a collection of elements that can be used to store and fetch dynamically. C# List preserves the index of elements in it. C# List can have duplicate elements. In C#, you can access List in System.Collections.Generic. Initialize C# List You can declare a C# List and add elements to it. Or you can define a C# list with initial values.


(0 can be whatever number)

or (gets a list of students and their house numbers) :

var houseNumbers = GetStudents().Select((student) => 
    String.Format("Student: {0} - {1}", 
    student.StudentId, student.Address.HouseNumber));
foreach(var entry in houseNumbers) Console.WriteLine(entry);

C#, FindAll(Predicate<T>) Method is used to get all the elements that match the conditions defined by the specified predicate. Properties of List: It is� C# Access List Elements In c#, we have different ways to access list elements i.e. either by using index positions or by iterating through the list using for / foreach loops. Following is the example of accessing list elements in different ways.

C#, List Constructor. Creates an empty list (of integer values). var list = new List<int> � C# - List<T> Adding an Array in a List. Use the AddRange () method to add all the elements from an array or another collection to Accessing a List using LINQ. The List<T> implements the IEnumerable interface. So, we can query a list using LINQ query Remove Elements from List. Use the Remove ()

C# List, You must import this namespace in your project to access the List<T> class. However, when more than 5 elements are added to the list,� List<T>.Item[Int32] Property is used to gets or sets the element at the specified index.. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot.

C# List Most Asked Questions, We can loop over the List elements in reverse with a for-loop. Start: For a reverse loop, we must access the last element first, so we get the Count and subtract one � To access any element from an array we need to access the array using index name. This can be done by placing an index of the element inside a square bracket preceded by the array name. For Example,if we have initialized and declared the following array: string[ ] student = {“student1”, “student2”, “student3”};

  • As Student doesn't override ToString() this will simply print the name of the class. Not really a useful answer.
  • @slugster Of course, I assume op knows to read s.BlahBlah etc
  • It will print the type name of the class, not its properties.
  • You've changed your code but it's still wrong - you want Console.WriteLine(id);