So when I return an object, under the covers I think it's returning the memory address to that object (or an object containing the memory address) which you can reference and use.
But what is actually happening when you return a func?
How does your app know which instance of an object to use with that func?
My instinct tells me that an object instance reference is passed along with the func but is that what is actually happening?
I can't seem to find much on this topic.
Edit: To clarify, I am asking when a method returns a func
A Func in C# is a way to define a method in-line that has a return value. There is a similar concept of an Action that doesn't have a return value, but we'll get to that in a sec. The return value's type is always the last generic parameter on the Func 's definition.
The following example uses Func to add values. int Sum(int x, int y) { return x + y; } Func<int, int, int> add = Sum; int res = add(150, 10); Console. WriteLine(res); We have a Sum method which adds two values.
Declares an extension function that is visible everywhere.
Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything. Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).
A Func
is a delegate -- a type of object that is associated with a specific method signature and is usually bound to a specific method with that signature. This might be either a static
or an instance method and it might be generic or not; in all cases, the delegate instance holds all the information required to call the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With