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. C#
  4. Predicate Conditions for Find, First...Methods

Predicate Conditions for Find, First...Methods

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

    Hi there, I´m trying to understand how Methods with Predicate and Conditions work; it´s a little confusing for me. For example, we have a 10-positions array of bytes, such as

    public byte[] Client_ID_List { get; private set; }

    , it´s initialized by:

    Client_ID_List = new byte[8];

    After a while, I want to find the first position in this array that has '0' value. I have seen that First method returns the first element that satisfies a condition, but I don´t really understand how Predicate argument works. Any help or simple example will be very useful. Or maybe any other idea to solve this issue. Kind Regards, D.

    W 1 Reply Last reply
    0
    • S share_holder

      Hi there, I´m trying to understand how Methods with Predicate and Conditions work; it´s a little confusing for me. For example, we have a 10-positions array of bytes, such as

      public byte[] Client_ID_List { get; private set; }

      , it´s initialized by:

      Client_ID_List = new byte[8];

      After a while, I want to find the first position in this array that has '0' value. I have seen that First method returns the first element that satisfies a condition, but I don´t really understand how Predicate argument works. Any help or simple example will be very useful. Or maybe any other idea to solve this issue. Kind Regards, D.

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      A predicate is just a method that returns a boolean value. Thus to use a predicate in a .First query, you can either use a Lambda expression

      byte FirstZeroByte()
      {
      return ClientIdList.First((b) => b == 0);
      }

      or pass a method to the query that returns a boolean

      bool IsZeroByte(byte byteToCheck)
      {
      if (byteToCheck == 0)
      return true;
      return false;
      }

          byte FirstZeroByte()
          {
              return ClientIdList.First(IsZeroByte);
          }
      

      Hope this helps

      Live for today. Plan for tomorrow. Party tonight!

      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