title: How to iterate through ArrayList and cast the returned object into another object
body: <p>(this is an academic problem)</p>
  
  <p>I created an Arraylist to hold student objects. Now I need to cast them back to original type and print out on the console.  I need to create a method in an object that would loop through all objects (after they have been cast back) and print them out -   </p>
  
  <p>How do I cast a returned object from the ArrayList to a (Student) object.</p>
  
  <p>In course object I created an ArrayList called studentarraylist</p>
  
  <pre><code>public class Course
          {
              ArrayList studentarraylist = new ArrayList();
  </code></pre>
  
  <p>In that course object I created a method to add student</p>
  
  <pre><code>public void AddStudent(Student student)
              {
                  studentarraylist.Add(student);          
               }
  </code></pre>
  
  <p>In main - I added a student object to the ArrayList using that method</p>
  
  <pre><code>course.AddStudent(student1);
  </code></pre>
  
  <p>Now I need to create a method in Course to cast objects back to original type, using a foreach loop iterate over the Students in the ArrayList and output their first and last names to the console window.  I got a bit mixed up as I don't have access to some items in the method - when I created them in main.</p>
  
  <pre><code>public void liststudents(ArrayList studentarraylist)
              {
                  Course studentoneout = (Course)studentarraylist[0];
  
  
                   for (int i = 0; i &lt; studentarraylist.Count; ++i)
                  {
                      //Console.WriteLine(studentarraylist[i]);
                      Console.WriteLine("student first name", studentarraylist.ToString());
                      Console.WriteLine("");
                  }
              }
  </code></pre>
  
  <p>edit *** </p>
  
  <pre><code>public class Course
          {
              ArrayList studentarraylist = new ArrayList();
  
              private string courseName;
              //Create a course name          
              public string CourseName
              {
                  get { return courseName; }
                  set { courseName = value; }
              }       
  
              //add student method
              public void AddStudent(Student student)
              {
                  studentarraylist.Add(student);          
               }
  
  
  
               public void liststudents(ArrayList studentarraylist)
              {
  
                  for (int i = 0; i &lt; studentarraylist.Count; i++)
                  {
                      Student currentStudent = (Student)studentarraylist[i];
                      Console.WriteLine("Student First Name: {0}", currentStudent.FirstName); // Assuming FirstName is a property of your Student class
                      Console.WriteLine("Student Last Name: {0}", currentStudent.LastName);
                      // Output what ever else you want to the console.
                  }
  
                   // Course studentoneout = (Course)studentarraylist[0];
  
  
  
  
  
  //                 for (int i = 0; i &lt; studentarraylist.Count; ++i)
   //               {
    //                  Student s = (Student)studentarraylist[i];
  
     //             }
              }
  </code></pre>