Func and Cannot implicitly convert type
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.I开发者_Go百科nc())
.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
I believe your method should be something like this:
public Cat ToFeed(Func<int, bool> predicate, Action<Cat> action)
{
if (predicate(Weith)) // By the way, it is properly the word Weight you're looking for :)
action(this);
return this;
}
predicate(Weith)
executes the function predicate
with the value of the property Weith
. A Func<int, bool>
is not in itself a bool (true/false - which is what an if-sentence expects.) However, it´s something you can execute, with an int as argument, to obtain a bool, therefore you need (arguments)
.
Updated:
I'm not entirely sure of what you want. But you could do as you say yourself and use a bool
instead of a Func<*>
. And if you want your evaluation of LikeMilk to be delayed, you can say like this:
public Cat ToFeed(Func<bool> predicate, Action<Cat> action)
{
if (predicate())
action(this);
return this;
}
and
var cat = new Cat().ToFeed(() => LikeMilk(1), c => c.Inc())
.ToFeed(() => LikeMilk(2), c => c.Inc());
But that doesn't make much sense because your method LikeMilk
will return the same thing delay-executed or not.
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.
You send result of delegate call instead of the delegate, try this version of your code
class Program
{
static void Main(string[] args)
{
var cat = new Cat();
cat
.ToFeed(1, LikeMilk, c.Inc)
.ToFeed(2, LikeMilk, 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(int liters, Func<int, bool> predicate, Action<Cat> action)
{
if (predicate(liters))
action(this);
return this;
}
}
}
精彩评论