Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c# covert a for loop to for each (arraylist)

c# covert a for loop to for each (arraylist)

Scheduled Pinned Locked Moved C#
questioncsharp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aindriu Mac Giolla Eoin
    wrote on last edited by
    #1

    hi, I have a for loop that works but I need to implement a foreach loop. How do I change to a foreach loop ? here is the existing working for loop in a method

    public void liststudents()
    {

               for (int i = 0; i < studentarraylist.Count; i++)
               {
                   // Cast the object from the ArrayList to Student
                   Student currentStudent = (Student)studentarraylist\[i\];
                   Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
    
    
    
               }
           }
    

    Also, does the above code indeed cast the returned object from the ArrayList to a Student object ?

    P Richard DeemingR M 3 Replies Last reply
    0
    • A Aindriu Mac Giolla Eoin

      hi, I have a for loop that works but I need to implement a foreach loop. How do I change to a foreach loop ? here is the existing working for loop in a method

      public void liststudents()
      {

                 for (int i = 0; i < studentarraylist.Count; i++)
                 {
                     // Cast the object from the ArrayList to Student
                     Student currentStudent = (Student)studentarraylist\[i\];
                     Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
      
      
      
                 }
             }
      

      Also, does the above code indeed cast the returned object from the ArrayList to a Student object ?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Aindriu Mac Giolla Eoin wrote:

      change to a foreach loop

      Why? For loops perform better.

      Aindriu Mac Giolla Eoin wrote:

      does the above code indeed cast the returned object

      Looks like it; why do you ask?

      A 1 Reply Last reply
      0
      • P PIEBALDconsult

        Aindriu Mac Giolla Eoin wrote:

        change to a foreach loop

        Why? For loops perform better.

        Aindriu Mac Giolla Eoin wrote:

        does the above code indeed cast the returned object

        Looks like it; why do you ask?

        A Offline
        A Offline
        Aindriu Mac Giolla Eoin
        wrote on last edited by
        #3

        I need a foreach loop to satisfy a requirement even though a for loop might perform better. I was looking at [microsoft link] but could get the cast work as well I wanted to double check that the returned object was casted to its orignal type thanks !

        1 Reply Last reply
        0
        • A Aindriu Mac Giolla Eoin

          hi, I have a for loop that works but I need to implement a foreach loop. How do I change to a foreach loop ? here is the existing working for loop in a method

          public void liststudents()
          {

                     for (int i = 0; i < studentarraylist.Count; i++)
                     {
                         // Cast the object from the ArrayList to Student
                         Student currentStudent = (Student)studentarraylist\[i\];
                         Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
          
          
          
                     }
                 }
          

          Also, does the above code indeed cast the returned object from the ArrayList to a Student object ?

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Try this:

          foreach (Student currentStudent in studentarraylist)
          {
          Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
          }

          You should try to avoid the ArrayList class[^], as it's not strongly typed. Use the List<T> class[^] (or one of the other generic collection types) instead. Generics (C# Programming Guide)[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          A 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Try this:

            foreach (Student currentStudent in studentarraylist)
            {
            Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
            }

            You should try to avoid the ArrayList class[^], as it's not strongly typed. Use the List<T> class[^] (or one of the other generic collection types) instead. Generics (C# Programming Guide)[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            A Offline
            A Offline
            Aindriu Mac Giolla Eoin
            wrote on last edited by
            #5

            thanks for that, i know its not strongly typed and could use something else but I need to use it for a question.

            1 Reply Last reply
            0
            • A Aindriu Mac Giolla Eoin

              hi, I have a for loop that works but I need to implement a foreach loop. How do I change to a foreach loop ? here is the existing working for loop in a method

              public void liststudents()
              {

                         for (int i = 0; i < studentarraylist.Count; i++)
                         {
                             // Cast the object from the ArrayList to Student
                             Student currentStudent = (Student)studentarraylist\[i\];
                             Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
              
              
              
                         }
                     }
              

              Also, does the above code indeed cast the returned object from the ArrayList to a Student object ?

              M Offline
              M Offline
              Marjan Venema
              wrote on last edited by
              #6

              While a for loop may perform better, a foreach loop reads better and is less prone to off-by-one errors. That is why I have come to prefer them everywhere except _proven_ performance bottlenecks.

              foreach (Student currentStudent in studentarraylist)
              {
              Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
              }

              --- http://softwareonastring.com

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups