Why methods return just one kind of parameter in normal conditions? [closed]

╄→гoц情女王★ 提交于 2021-02-04 08:41:29

问题


i experienced this questions in a during the job interview, but i cant answered it and i cant find the answer after. They asked me 'Why methods return just one kind of parameter in normal conditions in C#? (ref-out is extra way to return multiple parameters. but it is not a normal condition)'

-Tuple, list, dictionary, ext. => after all they are only for one kind parameters.


回答1:


"Why methods return just one kind of parameter in normal conditions?"

Because of the behavior of microprocessors and history of assembly language.

In OOP, for example in C#, on an Intel-like computer, a method is nothing than a procedure in machine code.

A procedure returns nothing and take nothing at all: it is just a goto, a jmp or a call that is an advanced jmp from first CPU generations.

So to create the CPU CALL the stack was invented to pass parameters as well as to get a resulting register value (so it is named a function in high-level languages).

Hence when we call a method, in fact (the compiler generates machine code for that), we PUSH before in the stack the values and/or the references for each parameters having only the maximum size of the platform registers (x32 = 16bits and x64 = 64bits - using less is like using one way on a multi-way road).

At the beginning of the procedure, we POP the values (the compiler generates that).

And at the end of the machine code of the procedure, we have a RET assembly opcode/mnemonic instruction.

If a return value is expected, before returning, it is PUSHed in the stack to be used by the caller where we return that POP it.

Using ref keyword is passing the memory pointer directly used by the procedure: we PUSH a pointer, CALL the proc, POP the pointer, use it, RET, and that's all, but less optimized as using the CPU internal stack is quicker than external memory access.

In addition to that, using the new C# features to return tuples, in fact, it returns a pointer (4 or 8 bytes) to the memory anonymous data structure, like if it was an instance reference of a class or struct object. It is usefull when we don't want to create a dedicated class or struct...

This is the only way to do programming, because a mathematical function only returns one value or a set of values, and in this last case, we need to get the set, to work on, not to be submerged by the values.

So the underlying answer to this question comes from the very structure of mathematical science, and in particular the set theory and the numbers theory.

.NET OpCodes Class

Stack register

The Concept of Stack and Its Usage in Microprocessors

Introduction of Stack based CPU Organization

What is the role of stack in a microprocessor?

To understand better and to improve your skills in computing, you may found interesting to investigate what is assembly language and how work the CPU. You can start with IL and modern Intel but it may be simpler, formative and complementary to start from the past 8086 to i386/i486.




回答2:


In addition to Olivier Rogier's answer about the implementation of the language in machine code, also consider how method calls are used in the language. E.g.:

x = 2 * (someMethod() + 1)

If someMethod could return multiple values, which one should be used in this expression? If your reply is "the language could require you to say which one at the call site", then congrats, that's how tuples work - you're returning one value (a tuple) with multiple components, not returning multiple values.



来源:https://stackoverflow.com/questions/65888156/why-methods-return-just-one-kind-of-parameter-in-normal-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!