Differences between readonly Func and Method

7

Forgetting issues such as readability, what would be the differences between the following calls?

readonly Func = (T1, T2) = > {return default (TResult); }

private readonly Func<int, int, int> Soma = new Func<Func<int, int, int>>(() => {
    var total = default(int);
    return (numA, numB) => {
        total += numA + numB;
        return total;
    };
})();

Method (T1, T2) {return default (TResult); }

private int total = 0;
private int Soma()
{
    total += numA + numB;
    return total;
}
    
asked by anonymous 29.04.2016 / 13:58

1 answer

7

Basically the first is an anonymous method defined by a delegate ( User Guide ). In case the delegate is pre set with the signature used . Then your "content" can be "stored" in a variable, as it was done.

readonly was used to prevent your content from being changed. One of the great advantages of using delegates (in this case using lambda syntax) is to be able to swap content (which should be executed) and give flexibility to the application. In this specific case where this is not possible, I do not see an advantage, of course it depends on the context where it is used, it has situations that can be interesting, including avoiding some patterns like Strategy, for example.

The return is unnecessary there in the header.

The second is a normal method treated by language. It has better support for the compiler and can be used in more situations.

I remember that the first one has some generic usage limitations that the second does not have. The second, in general, will be called by Soma() and must be available in context. The first one will be called by the name of the variable that is holding a reference to this code body. And this can be passed on to other contexts. It's even possible to throw the normal method into a variable that expects a method with the same signature, but is less common.

In terms of results, nothing changes. The performance of the former is slightly lower because there is an indirection. It also consumes a little more memory.

A delegate is a type that has a pointer to the code to be executed and to the variables it encloses (in this case the total , but in the other example it has this cost as well). It is a given, it is an object, and every object occupies memory. The method call has to query this address stored before, causing the indirection and an extra processing cost. Also, being an indirection is more complicated, if not impossible to make certain optimizations. The direct method is all solved by the compiler and does not have overhead . See a test .

The local variables of a normal method are placed in the stack . In the delegate method they are in the heap (within this object created to support the delegate / closure ) since they need to be available in other contexts.

The first one has greater difficulty to choose in case you have methods with different signatures / default arguments . The decision is best in normal methods. I would not know how to go into detail without doing extensive research first.

I may be wrong, but I see little need for the syntax to be complex like this. I think the code could be simplified. It seems to me that the first one is trying to simulate a class as well.

    
29.04.2016 / 14:32