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. Web Development
  3. ASP.NET
  4. LIST<T>

LIST<T>

Scheduled Pinned Locked Moved ASP.NET
helpquestion
4 Posts 2 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.
  • M Offline
    M Offline
    Morgs Morgan
    wrote on last edited by
    #1

    Hi guys, what is the best way to check if a LIST contain any elements. I tried both:

    if(ListName.Count > 0)
    {

    }
    else
    {

    }
    //and

    if(ListName.Any())
    {

    }
    else
    {

    }

    but both of these give a runtime error if there is nothing(NULL) in the LIST on the if() statement. Please help. Regards Morgs...

    J 1 Reply Last reply
    0
    • M Morgs Morgan

      Hi guys, what is the best way to check if a LIST contain any elements. I tried both:

      if(ListName.Count > 0)
      {

      }
      else
      {

      }
      //and

      if(ListName.Any())
      {

      }
      else
      {

      }

      but both of these give a runtime error if there is nothing(NULL) in the LIST on the if() statement. Please help. Regards Morgs...

      J Offline
      J Offline
      JV9999
      wrote on last edited by
      #2

      NULL and ListName.Count are two separate things. NULL means the ListName is not initialized yet (so you can't access any properties either). ListName.Count > 0 means that the ListName object is initialized, but just doesn't contain any items. What you want to have is:

      if(ListName != null && ListName.Count > 0) { }
      or
      if(ListName != null && ListName.Any()) { }

      M 1 Reply Last reply
      0
      • J JV9999

        NULL and ListName.Count are two separate things. NULL means the ListName is not initialized yet (so you can't access any properties either). ListName.Count > 0 means that the ListName object is initialized, but just doesn't contain any items. What you want to have is:

        if(ListName != null && ListName.Count > 0) { }
        or
        if(ListName != null && ListName.Any()) { }

        M Offline
        M Offline
        Morgs Morgan
        wrote on last edited by
        #3

        i have initialised the LIST like so:

        LIST nxt = new LIST();
        nxt = dbh.GetNextPage(cId, "1.1");
        if (nxt.Count > 0)
        {

        }

        and should the LIST contain no records i get a runtime error: index was outside the bounds of the array....yak yak yak. same applies to when i use Any();

        J 1 Reply Last reply
        0
        • M Morgs Morgan

          i have initialised the LIST like so:

          LIST nxt = new LIST();
          nxt = dbh.GetNextPage(cId, "1.1");
          if (nxt.Count > 0)
          {

          }

          and should the LIST contain no records i get a runtime error: index was outside the bounds of the array....yak yak yak. same applies to when i use Any();

          J Offline
          J Offline
          JV9999
          wrote on last edited by
          #4

          Then the GetNextPage method returns NULL and not an empty list.

          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