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. Other Discussions
  3. The Weird and The Wonderful
  4. Revolutionary new way to iterate indexable collections. Who needs to know how many elements they have?

Revolutionary new way to iterate indexable collections. Who needs to know how many elements they have?

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharptutorialquestion
7 Posts 5 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.
  • V Offline
    V Offline
    Vladimir Svyatski
    wrote on last edited by
    #1

    Recently I encountered brand new look at standard boring indexable collections like arrays or lists in code I worked on. To be more precise, at ways we usually use to iterate them. We no longer need properties or functions like: Length, Size, Count. See how it goes (example demonstrating the idea):

    int[] someArray = new int[] {/*element initialization*/};
    for (int i = 0; /*note, the loop is infinite here*/; i++)
    {
    try
    {
    int item = someArray[i];
    //do some stuff here
    }
    catch(Exception e)
    {
    break;//Bingo, we're done!
    }
    }

    Smell the power! Lenght, Count, Size, etc. They are obsolete now. :) We will iterate an indexable collection item by item until it crashes and that means our iteration is finished. That's what try/catch block was really intended for! P.S. int[] was used for demonstration purposes. You can try it at home with any indexable collection you want (in .NET, for example, any collection implementing IList or IList<T> will do just fine).

    U B K D 4 Replies Last reply
    0
    • V Vladimir Svyatski

      Recently I encountered brand new look at standard boring indexable collections like arrays or lists in code I worked on. To be more precise, at ways we usually use to iterate them. We no longer need properties or functions like: Length, Size, Count. See how it goes (example demonstrating the idea):

      int[] someArray = new int[] {/*element initialization*/};
      for (int i = 0; /*note, the loop is infinite here*/; i++)
      {
      try
      {
      int item = someArray[i];
      //do some stuff here
      }
      catch(Exception e)
      {
      break;//Bingo, we're done!
      }
      }

      Smell the power! Lenght, Count, Size, etc. They are obsolete now. :) We will iterate an indexable collection item by item until it crashes and that means our iteration is finished. That's what try/catch block was really intended for! P.S. int[] was used for demonstration purposes. You can try it at home with any indexable collection you want (in .NET, for example, any collection implementing IList or IList<T> will do just fine).

      U Offline
      U Offline
      Uros Calakovic
      wrote on last edited by
      #2

      VUnreal wrote:

      You can try it at home

      Shouldn't that be 'don't try this at home, leave it to the trained professionals'?

      The bearing of a child takes nine months, no matter how many women are assigned.

      1 Reply Last reply
      0
      • V Vladimir Svyatski

        Recently I encountered brand new look at standard boring indexable collections like arrays or lists in code I worked on. To be more precise, at ways we usually use to iterate them. We no longer need properties or functions like: Length, Size, Count. See how it goes (example demonstrating the idea):

        int[] someArray = new int[] {/*element initialization*/};
        for (int i = 0; /*note, the loop is infinite here*/; i++)
        {
        try
        {
        int item = someArray[i];
        //do some stuff here
        }
        catch(Exception e)
        {
        break;//Bingo, we're done!
        }
        }

        Smell the power! Lenght, Count, Size, etc. They are obsolete now. :) We will iterate an indexable collection item by item until it crashes and that means our iteration is finished. That's what try/catch block was really intended for! P.S. int[] was used for demonstration purposes. You can try it at home with any indexable collection you want (in .NET, for example, any collection implementing IList or IList<T> will do just fine).

        B Offline
        B Offline
        BillW33
        wrote on last edited by
        #3

        Yes, that will work. However, I sincerely hope to not see it in any real code. :)

        Just because the code works, it doesn't mean that it is good code.

        1 Reply Last reply
        0
        • V Vladimir Svyatski

          Recently I encountered brand new look at standard boring indexable collections like arrays or lists in code I worked on. To be more precise, at ways we usually use to iterate them. We no longer need properties or functions like: Length, Size, Count. See how it goes (example demonstrating the idea):

          int[] someArray = new int[] {/*element initialization*/};
          for (int i = 0; /*note, the loop is infinite here*/; i++)
          {
          try
          {
          int item = someArray[i];
          //do some stuff here
          }
          catch(Exception e)
          {
          break;//Bingo, we're done!
          }
          }

          Smell the power! Lenght, Count, Size, etc. They are obsolete now. :) We will iterate an indexable collection item by item until it crashes and that means our iteration is finished. That's what try/catch block was really intended for! P.S. int[] was used for demonstration purposes. You can try it at home with any indexable collection you want (in .NET, for example, any collection implementing IList or IList<T> will do just fine).

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          Jesus H. Christ. That's so appalling, I thought I'd investigate, so I ran this testbed:

          static int listSize = 10000000;
          static int tests = 10;
          static List list = new List();

          static void LoopClassic()
          {
          DateTime start= DateTime.Now;
          int f;
          for(int i= 0; i< tests; i++)
          {
          for(int j = 0; j < list.Count; j++)
          f = list[j];
          }
          TimeSpan elapsed = DateTime.Now.Subtract(start);
          Console.WriteLine("Classic: {0}", elapsed.TotalMilliseconds );
          }

          static void LoopCrufty()
          {
          DateTime start = DateTime.Now;
          int f;

          for (int i = 0; i < tests; i++)
          {
              for(int j=0;true;j++)
                  try
                  {
                      f = list\[j\];
                  }
                  catch
                  {
                      break;
                  }
          }
          TimeSpan elapsed = DateTime.Now.Subtract(start);
          Console.WriteLine("Crufty: {0}", elapsed.TotalMilliseconds);
          

          }

          static void Main(string[] args)
          {
          for (int i = 0; i < listSize; i++)
          list.Add(i);
          LoopClassic();
          LoopCrufty();
          Console.ReadKey();
          }

          The Crufty method was faster for the large list (over several runs in release mode)@ Classic~1000 ms, Crufty ~650ms. I changed the list size to 1,000,000 and the normal loop ran quicker (130ms vs 160). Still not justified though! [Edit] This is quicker for loops <= 10,000,000 or so!

          static void LoopClassic2()
          {
          DateTime start = DateTime.Now;
          int f;
          for (int i = 0; i < tests; i++)
          {
          int count = list.Count;
          for (int j = 0; j < count; j++)
          f = list[j];
          }
          TimeSpan elapsed = DateTime.Now.Subtract(start);
          Console.WriteLine("Classic2: {0}", elapsed.TotalMilliseconds);
          }

          Sort of a cross between Lawrence of Arabia and Dilbert.[^]
          -Or-
          A Dead ringer for Kate Winslett[^]

          modified on Saturday, April 16, 2011 12:58 PM

          V 1 Reply Last reply
          0
          • K Keith Barrow

            Jesus H. Christ. That's so appalling, I thought I'd investigate, so I ran this testbed:

            static int listSize = 10000000;
            static int tests = 10;
            static List list = new List();

            static void LoopClassic()
            {
            DateTime start= DateTime.Now;
            int f;
            for(int i= 0; i< tests; i++)
            {
            for(int j = 0; j < list.Count; j++)
            f = list[j];
            }
            TimeSpan elapsed = DateTime.Now.Subtract(start);
            Console.WriteLine("Classic: {0}", elapsed.TotalMilliseconds );
            }

            static void LoopCrufty()
            {
            DateTime start = DateTime.Now;
            int f;

            for (int i = 0; i < tests; i++)
            {
                for(int j=0;true;j++)
                    try
                    {
                        f = list\[j\];
                    }
                    catch
                    {
                        break;
                    }
            }
            TimeSpan elapsed = DateTime.Now.Subtract(start);
            Console.WriteLine("Crufty: {0}", elapsed.TotalMilliseconds);
            

            }

            static void Main(string[] args)
            {
            for (int i = 0; i < listSize; i++)
            list.Add(i);
            LoopClassic();
            LoopCrufty();
            Console.ReadKey();
            }

            The Crufty method was faster for the large list (over several runs in release mode)@ Classic~1000 ms, Crufty ~650ms. I changed the list size to 1,000,000 and the normal loop ran quicker (130ms vs 160). Still not justified though! [Edit] This is quicker for loops <= 10,000,000 or so!

            static void LoopClassic2()
            {
            DateTime start = DateTime.Now;
            int f;
            for (int i = 0; i < tests; i++)
            {
            int count = list.Count;
            for (int j = 0; j < count; j++)
            f = list[j];
            }
            TimeSpan elapsed = DateTime.Now.Subtract(start);
            Console.WriteLine("Classic2: {0}", elapsed.TotalMilliseconds);
            }

            Sort of a cross between Lawrence of Arabia and Dilbert.[^]
            -Or-
            A Dead ringer for Kate Winslett[^]

            modified on Saturday, April 16, 2011 12:58 PM

            V Offline
            V Offline
            Vladimir Svyatski
            wrote on last edited by
            #5

            You made me laugh. You even managed to test the stuff :) Actually I can even say how this wonderful code has emerged. One "colleague" of mine (I don't consider him as a real colleague to be honest, 'cause he produces sh*t regularly) was too lame to read documentation. Every wise enough human being must have a thought in his/her mind that if a collection's element can be retrieved by index, there must be some kind of function/property like Size, Count, Length, etc. allowing us to know when to stop. So that "guru" had found function that retrieves an object by index, but had failed to scroll the documentation a little further to find number of elements. And little genius invented the construction you saw above. I had no words when I first saw the stuff.

            K 1 Reply Last reply
            0
            • V Vladimir Svyatski

              You made me laugh. You even managed to test the stuff :) Actually I can even say how this wonderful code has emerged. One "colleague" of mine (I don't consider him as a real colleague to be honest, 'cause he produces sh*t regularly) was too lame to read documentation. Every wise enough human being must have a thought in his/her mind that if a collection's element can be retrieved by index, there must be some kind of function/property like Size, Count, Length, etc. allowing us to know when to stop. So that "guru" had found function that retrieves an object by index, but had failed to scroll the documentation a little further to find number of elements. And little genius invented the construction you saw above. I had no words when I first saw the stuff.

              K Offline
              K Offline
              Keith Barrow
              wrote on last edited by
              #6

              VUnreal wrote:

              You made me laugh.

              Glad to be of service :-)

              Sort of a cross between Lawrence of Arabia and Dilbert.[^]
              -Or-
              A Dead ringer for Kate Winslett[^]

              1 Reply Last reply
              0
              • V Vladimir Svyatski

                Recently I encountered brand new look at standard boring indexable collections like arrays or lists in code I worked on. To be more precise, at ways we usually use to iterate them. We no longer need properties or functions like: Length, Size, Count. See how it goes (example demonstrating the idea):

                int[] someArray = new int[] {/*element initialization*/};
                for (int i = 0; /*note, the loop is infinite here*/; i++)
                {
                try
                {
                int item = someArray[i];
                //do some stuff here
                }
                catch(Exception e)
                {
                break;//Bingo, we're done!
                }
                }

                Smell the power! Lenght, Count, Size, etc. They are obsolete now. :) We will iterate an indexable collection item by item until it crashes and that means our iteration is finished. That's what try/catch block was really intended for! P.S. int[] was used for demonstration purposes. You can try it at home with any indexable collection you want (in .NET, for example, any collection implementing IList or IList<T> will do just fine).

                D Offline
                D Offline
                DragonsRightWing
                wrote on last edited by
                #7

                I wonder what will happen when //do some stuff here throws an exception??? ;-)

                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