函数调用C# [英] Function calling in C#

查看:67
本文介绍了函数调用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



Hi All,

MyClass obj = new MyClass();
obj.Function();
obj.x;



我认为函数和成员变量位于单独的内存(堆栈和堆)中,函数对于类的所有实例都是通用的。所以object(obj)如何访问函数和成员?


I think functions and member variables are in separate memory(stack and heap) and functions are common to all instance of a class. so how object (obj) can access both function and members?

推荐答案

如果方法非常小,现代编译器通常会内联它们而不是制作经典请致电如下:



*为您的可执行文件创建一个新进程。该进程有一个堆栈段,包含每个线程的堆栈,一个静态变量的数据段,一个称为堆的内存块,用于动态分配的内存,以及一个包含编译代码的代码段。

*您的代码被加载到代码段中,指令指针被设置为main()方法中的第一条指令,代码开始执行。

*对象obj是从堆中分配的。 obj的地址存储在堆栈中(每个线程都有自己的堆栈)。

* obj.Function()通过将返回地址放在堆栈上的main()来调用将指令指针更改为obj.Function()的代码的开头。返回地址与值1和2一起放在堆栈上。

* obj.Function()关闭堆栈并将指令指针设置为该值。



参考资料: http:/ /www.youtube.com/watch?v=FvpxXmEG1F8&feature=PlayList&p=9D558D49CA734A02&index=9 [ ^ ]
If the methods are very small, modern compilers will often "inline" them instead of making a classic call as below:

* A new process is created for your executable. That process has a stack segment containing each thread''s stack, a data segment for static variables as well as a memory block called the heap for dynamically allocated memory, and a code segment containing the compiled code.
* Your code is loaded into the code segment, the instruction pointer is set to the first instruction in your main() method, and the code begins executing.
* Object obj is allocated from the heap. The address of obj is stored on the stack (each thread has it''s own stack).
* obj.Function() is called by placing the return address to main() on the stack and changing the instruction pointer to the start of obj.Function()''s code. The return address is placed on the stack along with the values 1 and 2.
* obj.Function() off of the stack and setting the instruction pointer to that value.

Nice reference at: http://www.youtube.com/watch?v=FvpxXmEG1F8&feature=PlayList&p=9D558D49CA734A02&index=9[^]


正如您所注意到的,我之前回答过: 什么使静态方法可以访问? [ ^ ]。



-SA
As you correctly notices, I answered before: What makes static methods accessible?[^].

—SA


这篇关于函数调用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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