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. Using Generic Method to Determine if All Elements in an Array is Zero

Using Generic Method to Determine if All Elements in an Array is Zero

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

    I want to know how to do that. I can use foreach and for loop, but I prefer to use the array class. Assume I have my array like

    int[] arrayData = { 1, 3, 5, 0, 6 };

    By using .All generic method, it is possible to do something like that to determine if all elements are zero

    arrayData.All //this is where I have the problem

    So how can I use the .All to determine if all elements in the array is 0

    A C 2 Replies Last reply
    0
    • M mfcuser

      I want to know how to do that. I can use foreach and for loop, but I prefer to use the array class. Assume I have my array like

      int[] arrayData = { 1, 3, 5, 0, 6 };

      By using .All generic method, it is possible to do something like that to determine if all elements are zero

      arrayData.All //this is where I have the problem

      So how can I use the .All to determine if all elements in the array is 0

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      The MSDN has samples, but basically you write a predicate method that returns what you want to return. For example, a search method would involve you writing the code that checks each element. Under the hood, it's not much different at the end of the day, the code that runs will use for each to check each element.

      Christian Graus Driven to the arms of OSX by Vista.

      1 Reply Last reply
      0
      • M mfcuser

        I want to know how to do that. I can use foreach and for loop, but I prefer to use the array class. Assume I have my array like

        int[] arrayData = { 1, 3, 5, 0, 6 };

        By using .All generic method, it is possible to do something like that to determine if all elements are zero

        arrayData.All //this is where I have the problem

        So how can I use the .All to determine if all elements in the array is 0

        A Offline
        A Offline
        Adam Maras
        wrote on last edited by
        #3

        arrayData.All(item => item == 0);

        M 1 Reply Last reply
        0
        • A Adam Maras

          arrayData.All(item => item == 0);

          M Offline
          M Offline
          mfcuser
          wrote on last edited by
          #4

          How do you declare item in this case?

          A 1 Reply Last reply
          0
          • M mfcuser

            How do you declare item in this case?

            A Offline
            A Offline
            Adam Maras
            wrote on last edited by
            #5

            item is an inline-declared parameter. You shouldn't have to explicitly declare it anywhere. Enumerable.All should be able to determine the type of the parameter at compile-time. Note: I'm not in front of my compiler right now, but it's possible (?) that you may have to explicitly provide the parameter with a type. In that case, I believe the syntax would be:

            arrayData.All((int item) => item == 0);

            (disclaimer: this is all coming from memory, so I might not be spot-on :doh:)

            M 2 Replies Last reply
            0
            • A Adam Maras

              item is an inline-declared parameter. You shouldn't have to explicitly declare it anywhere. Enumerable.All should be able to determine the type of the parameter at compile-time. Note: I'm not in front of my compiler right now, but it's possible (?) that you may have to explicitly provide the parameter with a type. In that case, I believe the syntax would be:

              arrayData.All((int item) => item == 0);

              (disclaimer: this is all coming from memory, so I might not be spot-on :doh:)

              M Offline
              M Offline
              mfcuser
              wrote on last edited by
              #6

              It seems to work fine now

              1 Reply Last reply
              0
              • A Adam Maras

                item is an inline-declared parameter. You shouldn't have to explicitly declare it anywhere. Enumerable.All should be able to determine the type of the parameter at compile-time. Note: I'm not in front of my compiler right now, but it's possible (?) that you may have to explicitly provide the parameter with a type. In that case, I believe the syntax would be:

                arrayData.All((int item) => item == 0);

                (disclaimer: this is all coming from memory, so I might not be spot-on :doh:)

                M Offline
                M Offline
                mfcuser
                wrote on last edited by
                #7

                Your method works fine, but it looks like it is not possible to check if all elements equal 0 in a 2D array. I couldn't find a way to do that for a 2D array as I did for a 1D. Is there a way to check if all elements is 0 in a 2D array?

                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