none
Func<T, TResult> Delegate RRS feed

  • Question

  • I want to know that.which is good to use ,the Func<T, TResult> Delegate or your own delegate.

    Both can do same thing but  Func<T, TResult> parameters support contravariance mechanism.

    ex:

     Func<int , bool> fun =    result => {                                                
                                                    if (result > 0)
                                                        return true;
                                                    else
                                                        return false;
                                                };

    this can be done by creating own delegate as well like ... int del(bool b).

    Which one gives better performance or is there any guide line that which one should be used when?

    Monday, June 4, 2012 7:11 AM

Answers

  • There is no performance difference between those 2 ways. But, I recommend using Func<..> delegate because it is already present in BCL, so you do not have to write one more delegate. So, avoid some extra code :)


    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, June 4, 2012 8:42 AM

All replies

  • I guess the Func<T,Result> is beneficial when there is a task which is doing bulk of operation.

    Mark Answered, if it solves your question
    Rohit Arora

    Monday, June 4, 2012 7:26 AM
  • There is no performance difference between those 2 ways. But, I recommend using Func<..> delegate because it is already present in BCL, so you do not have to write one more delegate. So, avoid some extra code :)


    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, June 4, 2012 8:42 AM
  • There is no real difference other than with Func<T, TResult> you can pass lambda expression as a parameter without setting it to the delegate. So it just simplifies creating delegate to the method with signature there T is the input and TResult is the output without that you must define the delegate.

    Monday, June 4, 2012 8:46 AM
  • thanks Adavesh.. i agree that Func removes the overhead of creating a delegate.

    Lalit

    Monday, June 4, 2012 11:30 AM