为什么方法在正常情况下仅返回一种参数? [英] Why methods return just one kind of parameter in normal conditions?

查看:58
本文介绍了为什么方法在正常情况下仅返回一种参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在求职面试中遇到了这个问题,但是我无法回答,之后也找不到答案.他们问我:为什么在C#中正常情况下方法只返回一种参数?(ref-out是返回多个参数的额外方法.但这不是正常情况)'

-元组,列表,字典等.=>毕竟它们只是一种参数.

解决方案

为什么在正常情况下方法只返回一种参数?"

由于微处理器的行为和汇编语言的历史.

在OOP中,例如在C#中,在类似Intel的计算机上,方法只不过是机器代码中的过程.

过程什么也不返回,什么也不做:它只是goto,jmp或调用,它们是第一代CPU的高级jmp.

因此,为了创建CPU调用,发明了堆栈以传递参数以及获得结果注册值(因此它被高级语言命名为函数).

因此,当我们调用一个方法时,实际上(编译器会为此生成机器代码),我们在栈中之前按每个参数的值和/或引用仅具有平台寄存器的最大大小(x32 =16位和x64 = 64位-使用更少就像在多路道路上使用一种方式.

在过程开始时,我们将POP值(编译器会生成这些值).

在过程的机器代码结尾处,我们有一个RET汇编操作码/助记符指令.

如果期望返回值,则在返回之前,将其压入堆栈以供调用方使用,我们在该堆栈中返回该POP.

使用 ref 关键字传递过程直接使用的内存指针:我们推指针,调用proc,POP指针,使用RET,仅此而已,但优化程度不如使用CPU内部堆栈比访问外部存储器更快.

除此之外,实际上,使用新的C#功能返回元组,它还会返回指向内存匿名数据结构的指针(4或8个字节),就像它是类或struct对象的实例引用一样.当我们不想创建专用的类或结构时,这很有用.

这是进行编程的唯一方法,因为数学函数仅返回一个值或一组值,而在后一种情况下,我们需要获取该组值以进行处理,而不能被这些值所淹没

因此,此问题的根本答案来自数学科学的结构,尤其是设置理论数字理论.

.NET OpCodes类

堆栈注册

堆栈的概念及其在堆栈中的使用微处理器

基于堆栈的CPU组织介绍

堆栈在微处理器中的作用是什么?

为了更好地理解和提高您的计算技能,您可能会发现研究汇编语言以及 CPU 的工作方式.您可以从 IL 现代英特尔,但它可能更简单,更具格式性且补充以从过去开始 8086到i386/i486 .

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.

解决方案

"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.

这篇关于为什么方法在正常情况下仅返回一种参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆