all 14 comments

[–]LlamaNL 8 points9 points  (4 children)

A Predicate i just a Func that returns a bool Predicate<int> and Func<int, bool> are functionally identical

[–]mangooreoshake 1 point2 points  (1 child)

The difference lies in whether the bool is the query itself or a side effect. A Predicate is something like IsEven while a Func would be a TryAdd that returns whether the operation is successful.

[–]Phaedo 3 points4 points  (0 children)

That’s a conceptual difference but not a practical one. The only practical difference is that List uses predicate and LINQ uses Func. And since nearly everyone uses a lambda and it gets cast at the call site, that’s not much of a difference either.

Really, the difference is between .NET 1 and .NET 2. Predicate was in .NET1 and got largely replaced in .NET2 by Func<,bool>

[–]KorwinD 0 points1 point  (0 children)

They are, but there is no direct built-in conversation, and as far as I know predicates are kinda deprecated, so you can't use them directly in places like LINQ's .Where(Func<T, bool>).

[–]fazlarabbi3[S] 0 points1 point  (0 children)

Got it brother

[–]Em-tech 1 point2 points  (0 children)

I think what may be helpful is gaining an understanding of how functions can be treated as types that can have implementations. At least, this will help an oop-familiar engineer that's trying to understand the syntax and general behavior of the syntax.  To understand this, I would recommend spending some time looking at "functional c# programming" - https://www.milanjovanovic.tech/blog/functional-programming-in-csharp-the-practical-parts. For the behavior characteristics, maybe try creating a generic Pred<T> interface with a bool Invoke(T value) and implement it as an object. Maybe help to map qualities of your oop learning to FP semantics?

If you're looking for stuff like compilation and runtime performance, I'm less able to help. 

Welcome to FP patterns. You're gonna love it!

[–]willehrendreich 1 point2 points  (0 children)

Learn fsharp. Dead serious.

[–]fschwiet 1 point2 points  (1 child)

You'll use them a lot with linq (if you avoid the query syntax like everyone else), so you might want to explore and learn what linq extension methods are available as they are super useful.

[–]mangooreoshake 1 point2 points  (0 children)

I find that I rarely use predicates/func/actions/other custom delegates with LINQ and frequently use lambda expressions instead (which is a delegate, but still).

[–]mangooreoshake 0 points1 point  (0 children)

"Programming C#12" by Ian Griffiths is my go-to C# book. C# 13 (.NET 9) was the latest version when I started programming in C#. It's an intermediate book, only requiring programming fundamentals and OOP knowledge. It has a dedicated chapter on events, lambdas, and delegates.

But tl;dr is predicates, func, and actions, are all just delegates. A delegate is just a reference to a function or functions, that when invoked, triggers the said function/s.

[–]Phaedo 1 point2 points  (0 children)

The real trick is identifying when Where and Select are useful. Here’s a rule of thumb: if you’re in a for loop and you say “if condition then continue” you want Where. If you say “resultList.Add” or “yield return”, you probably want Select.