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. check array value exists

check array value exists

Scheduled Pinned Locked Moved C#
data-structurestutorial
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.
  • S Offline
    S Offline
    scottichrosaviakosmos
    wrote on last edited by
    #1

    i have a function which returns a string array. the array may or may not have value. how to check if the array has any value. In the below code how to check in if codition. i want to keep the looping option as last. I would like to check in 1 line statement. I tried indexof, contain and exits option but all of them are for checking a specific value in array and not to check the whole array if it has any value presence. below is the sample code. main() { string[]victory=war(); if(victory as any value except null) then { } else { } } public string[] war() { step 1..... return some string array }

    O OriginalGriffO 2 Replies Last reply
    0
    • S scottichrosaviakosmos

      i have a function which returns a string array. the array may or may not have value. how to check if the array has any value. In the below code how to check in if codition. i want to keep the looping option as last. I would like to check in 1 line statement. I tried indexof, contain and exits option but all of them are for checking a specific value in array and not to check the whole array if it has any value presence. below is the sample code. main() { string[]victory=war(); if(victory as any value except null) then { } else { } } public string[] war() { step 1..... return some string array }

      O Offline
      O Offline
      onelopez
      wrote on last edited by
      #2

      Use

      array.Any()

      . It's an extension method to

      IEnumerable<>

      if(array.Any(a => a!=null) ){
      // there's a non-null object here
      }

      http://msdn.microsoft.com/en-us/library/vstudio/bb337697(v=vs.100).aspx[^]

      S 1 Reply Last reply
      0
      • S scottichrosaviakosmos

        i have a function which returns a string array. the array may or may not have value. how to check if the array has any value. In the below code how to check in if codition. i want to keep the looping option as last. I would like to check in 1 line statement. I tried indexof, contain and exits option but all of them are for checking a specific value in array and not to check the whole array if it has any value presence. below is the sample code. main() { string[]victory=war(); if(victory as any value except null) then { } else { } } public string[] war() { step 1..... return some string array }

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Try:

        if (victory != null && victory.Length > 0)
        {
        ...
        }

        You looking for sympathy? You'll find it in the dictionary, between sympathomimetic and sympatric (Page 1788, if it helps)

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • O onelopez

          Use

          array.Any()

          . It's an extension method to

          IEnumerable<>

          if(array.Any(a => a!=null) ){
          // there's a non-null object here
          }

          http://msdn.microsoft.com/en-us/library/vstudio/bb337697(v=vs.100).aspx[^]

          S Offline
          S Offline
          scottichrosaviakosmos
          wrote on last edited by
          #4

          its working. cheers J.

          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