What is the difference between Func and delegate?

后端 未结 4 941
谎友^
谎友^ 2020-12-23 18:49

I see delegates in two forms:

A. Func convertMethod = lambda 

B. public delegate string convertMethod(string value);

4条回答
  •  再見小時候
    2020-12-23 19:05

    The code sample you have is confusing things a bit so let me try and clear it up. The following 2 items are delegate declarations. These are easy to spot because they will always contain the delegate keyword

    public delegate TReturn Func(Targ value);
    public delegate string convertMethod(string value);
    

    This line of code is assigning a value to a local which is typed to a delegate

    Func local = lambda;
    

    The above code is not limited to using just lambdas though. The value could also be a compatible method group or another delegate value.

    One other item to note is that even though Func and convertMethod are both delegates with identical signatures their values are not convertible to each other. For example the following is illegal

    Func local1 = ...;
    convertMethod local2 = local1; // Error!!!
    

提交回复
热议问题