Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5951029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:28:43+00:00 2026-05-22T17:28:43+00:00

So I have simple example of my code and I can’t find idea how

  • 0

So I have simple example of my code and I can’t find idea how to rewrite it.
Code:

 class Program
  {
    static void Main(string[] args)
    {
      var cat = new Cat();
      cat
        .ToFeed(arg => LikeMilk(1), c => c.Inc())
        .ToFeed(arg => LikeMilk(2), c => c.Inc());
    }

    private static bool LikeMilk(int liters)
    {
      return liters <1;
    }
  }

  public class Cat
  {
    private int Weith { get; set; }

    public void Inc()
    {
      Weith++;
    }

    public Cat ToFeed(Func<int, bool> predicate, Action<Cat> action)
    {
      if (predicate)  // Error: Cannot implicitly convert type 'System.Func<int,bool>' to 'bool' 
        action(this);
      return this;
    }
  }
}

Of course I can change signature ToFeed to ToFeed(bool predicate, Action<Cat> action), but I don’t want to do it.
I understand that I have to add int parameter to call of predicate, but I have added it in Main()

How can I solve this problem?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:28:43+00:00Added an answer on May 22, 2026 at 5:28 pm

    You’re passing a function value to ‘ToFeed’ and not a bool value. That’s why you need to actually invoke the function:

    if (predicate(this.liters))   //Check whether cat still wants milk. Yum!
        action(this);
    

    Edit:
    Now that I look closer at your code, I see that you hard coded the amount of liters in the main function and that thus the lambda-parameter arg is useless.

    You should change the code in your main method to:

    var cat = new Cat();
          cat
            .ToFeed(arg => LikeMilk(arg), c => c.Inc())
            .ToFeed(arg => LikeMilk(arg), c => c.Inc());
    

    Now the lambda actually uses the parameter passed. Then you can add a liters-Variable to your Cat-class which you pass to the predicate. See my updated code above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Can I have a simple example of an assembly code that when compiled into
I have this simple example I can't seems to get working : MERGE INTO
I have a simple code example @ http://jsbin.com/ukiwo3/edit It has 2 connected lists and
I have this simple example code: var request = mozIndexedDB.open('MyTestDatabase'); request.onsuccess = function(event){ var
I have seen simple example Ajax source codes in many online tutorials. What I
Does anyone have a simple example of implementing an async validation rule in csla?
I have this very simple example that I am using to learn structs in
Below I have a very simple example of what I'm trying to do. I
A simple example: Let's say I have one alias being sourced somewhere as: alias

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.