C #: How do sequential function calls work? (Efficiency Criss-Cross)

In C # it is usually written fairly objectively, for example:

MyObj obj = new MyObj(); MyReturn ret = obj.DoSomething(); AnotherReturn rett = ret.DoSomethingElse(); 

I could just write above:

 AnotherReturn rett = new MyObj().DoSomething().DoSomethingElse(); 

However, how does the stack frame work when you have a bunch of function calls in that order? The example is pretty simple, but imagine if I have 50+ function calls (this can happen with JavaScript like this (/ w jQuery)).

My assumption was that for each function call, a return address is performed (to the "point"?), And the return value (a new object with other methods) is immediately pumped into the next function call while returning the address. How does it work wrt getting a common return value (in this example, the return address will assign the final value to the rett function)? If I continued the chain of calls, would I, in the end, overflow? In this case, is it wiser to use an objective route (due to the "unnecessary" memory allocation?).

+6
source share
2 answers

This is exactly the same as if you were calling each method on a separate line, each time assigning the return value to a variable, and then using that variable to call the next method.

So your two samples are the same, efficient.

Do you have a reflector? You can try two methods and check the generated IL code to see exactly what the differences are.

+2
source

Although the 2 calls are the same, but if you have many "Points", then somewhere it’s the smell of the code (Law of Demeter).

See discussion below.

0
source

All Articles