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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. the fast ways to create a list

the fast ways to create a list

Scheduled Pinned Locked Moved Visual Basic
4 Posts 3 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.
  • D Offline
    D Offline
    desanti
    wrote on last edited by
    #1

    Hello ! Is there any fast way to create a list of obj1(Integer,string) , where the first field is an integer from 1-2000 , and the second field is the same string. , so the list should be : (1,"a") , (2,"a"),(3,"a")........(2000,"a"). I know that with a for loop can I create this , but i'm asking if there's a more fast way. Thank you !

    Richard DeemingR 1 Reply Last reply
    0
    • D desanti

      Hello ! Is there any fast way to create a list of obj1(Integer,string) , where the first field is an integer from 1-2000 , and the second field is the same string. , so the list should be : (1,"a") , (2,"a"),(3,"a")........(2000,"a"). I know that with a for loop can I create this , but i'm asking if there's a more fast way. Thank you !

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

      Depends what you mean by "fast". :) If you mean you want to save on typing, then try LINQ:

      Dim myList As List(Of obj1) = Enumerable.Range(1, 2000).Select(Function(i) New obj1(i, "a")).ToList()

      If you want the code to execute faster, then a For loop is probably your best bet. If you know the size of the list, then make sure you specify the initial capacity, to avoid having to reallocate the internal storage over and over again. List<T>.Capacity Property (System.Collections.Generic) | Microsoft Docs[^]


      "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

      D 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Depends what you mean by "fast". :) If you mean you want to save on typing, then try LINQ:

        Dim myList As List(Of obj1) = Enumerable.Range(1, 2000).Select(Function(i) New obj1(i, "a")).ToList()

        If you want the code to execute faster, then a For loop is probably your best bet. If you know the size of the list, then make sure you specify the initial capacity, to avoid having to reallocate the internal storage over and over again. List<T>.Capacity Property (System.Collections.Generic) | Microsoft Docs[^]


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

        D Offline
        D Offline
        desanti
        wrote on last edited by
        #3

        for loop is faster than Linq ? In other occasions , the for loop was very slower than Linq

        L 1 Reply Last reply
        0
        • D desanti

          for loop is faster than Linq ? In other occasions , the for loop was very slower than Linq

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          desanti wrote:

          In other occasions , the for loop was very slower than Linq

          Measured by you on a timer? One is a simple loop, the other a call to an interface to return an object on each iteration. Which of the two do you think would be faster? Also, instead of having a list, this would probably be done faster by casting the int to a string when you need it, instead of having it in memory in a non-array.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          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