Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,
I have one question in LINQ.

When we use where clause in LINQ it use Func<tsource> delegate.
C#
List<int> numbers = new List<int> { 5, 10, 20, 30, 4, 9, 8, 6 };
           var result = numbers.Where(x => x > 5);//Here where use Func<TSource in,Bool>


My question is that why LINQ not used Predicate<tsource> instead of Func<>?

Thanks
Dinesh Sharma
Posted
Updated 9-Jun-16 4:30am
v2
Comments
Philippe Mori 9-Jun-16 9:45am    
Do you realize that you wrote the same thing twice?

DRY: Don't repeat yourself.

See Don't repeat yourself. And note the allusion to WET (waste everyone's time) which is clearly what you are doing when you repeat the same thing twice.

1 solution

Because it's a special case of a Func, and the guidelines say "don't use predicates": LINQ Framework Design Guidelines[^]
"Do use the new LINQ types "Func<>" and "Expression<>" instead of custom delegates and predicates, when defining new APIs."
Not all the Linq methods could use a Predicate since they can use more than one parameter of different types.

There is a good explanation here: c# - Func vs. Action vs. Predicate - Stack Overflow[^]
 
Share this answer
 
Comments
BillWoodruff 9-Jun-16 23:05pm    
fyi: Jon Skeet's comment on that guideline:

"That's a bizarre guideline as written. Surely it should state "Do use the new LINQ types "Func<>" and "Action<>" [...]". Expression<> is a completely different thing."

in the comments on this SO thread:

http://stackoverflow.com/a/665513/133321

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900