What's so great about Func<> delegate?

前端 未结 6 646
迷失自我
迷失自我 2020-12-12 13:57

Sorry if this is basic but I was trying to pick up on .Net 3.5.

Question: Is there anything great about Func<> and it\'s 5 overloads? From the looks of it, I can

6条回答
  •  悲&欢浪女
    2020-12-12 14:26

    The Func family of delegates (and their return-type-less cousins, Action) are not any greater than anything else you'd find in the .NET framework. They're just there for re-use so you don't have to redefine them. They have type parameters to keep things generic. E.g., a Func is the same as a System.Predicate delegate. They were originally designed for LINQ.

    You should be able to just use the built-in Func delegate for any value-returning method that accepts up to 4 arguments instead of defining your own delegate for such a purpose unless you want the name to reflect your intention, which is cool.

    Cases where you would absolutely need to define your delegate types include methods that accept more than 4 arguments, methods with out, ref, or params parameters, or recursive method signatures (e.g., delegate Foo Foo(Foo f)).

提交回复
热议问题