- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
首先,如果听起来太蠢,我深表歉意。但我总是发现自己在问以下问题。
由于对象类是 .Net 中所有类的最终基类,而类是引用类型,因此引用类型存储在堆上,所以任何东西都如何进入堆栈。我知道原始类型存储在堆栈中,这就是我感到困惑的地方。
以int32为例,如果它是一个类,它的基类是object,那么它是一个引用类型,应该在堆上。它如何进入堆栈。
如果它不是一个类,那我们该怎么做
int i = 1;
object obj = i;
因为我不能做类似的事情
int i = 1;
MyClass myObj = i;
编辑
从我得到的答案来看,int 是结构而不是类。仍然令人困惑的是值类型的基类是引用类型(对象),而它的值类型不是引用类型。
出现的另一点是“属于类的值类型不会存储在堆栈中”,我想一切都在类中,甚至是控制台程序中的 Main 方法。对我来说,这意味着所有变量都以某种方式属于一个类?那么它们如何堆叠?
最佳答案
First of all I apologize if it sounds too dumb. but I always find myself asking the following questions.
这些都是很常见的问题。
Since object class is the ultimate base class for all classes in .Net and class is a reference type, reference types are stored on heap so how did anything even get to the stack.
你混淆了很多东西。让我们开始:
OK,现在我们知道有三种东西,其中两种是值。
现在让我们正确地说出你的句子:
The object class is the ultimate base class for all classes in .NET. All instances of classes are instances of reference types. All instances of reference types are stored on heap. How is anything stored on the stack?
既然我们的问题措辞正确,答案就很清楚了。 引用和值类型的实例可以进入堆栈。
让我们继续。
I know primitive types get stored on stack and that's where I get confused.
从你的词汇表中消除“原始类型”这个词;这是没有意义的。让我们换个说法:
I know that instances of value types get stored on stack
不,您不知道知道,因为要算作知识,陈述必须是真实的。这种说法是错误的。值存储在变量中。变量要么是长生命周期的,要么是短生命周期的。短期变量进入堆栈。长生命周期变量进入堆。
Take int32 for instance, if its a class and its base class is object, then its a reference type, should be on heap.
Int32 不是一个类。这是一个结构。它不是引用类型。
Still the confusion is how a value type's base class is a reference type (object) and its a value type not a reference type.
玛丽是一位母亲。她是女性。因此,她所有的 child 都是女性吗?
How does it go to stack.
当整数存储在短期变量中时,该整数进入堆栈。
当引用存储在短期变量中时,引用进入堆栈。
值是引用还是值类型的实例无关紧要;重要的是变量存活多长时间?
If its not a class then how can we do this
int i = 1;
object obj = i;
好问题。生成一个特殊的类。将其视为:
class BoxedInt
{
int value;
public BoxedInt(int v) { this.value = v; }
public int Unbox() { return this.value; }
}
那么你的程序片段是:
int i = 1;
object obj = new BoxedInt(i);
然后
int j = (int)obj;
与
相同int j = ((BoxedInt)obj).Unbox();
Another point that came up is that "value-types that belong to a class do not get stored on the stack
更好的说法是:引用类型的字段是长生命周期变量。因此它们存储在堆上。
I guess everything is inside a class, even the Main method inside a console Program.
是的,所有代码都在类或结构中。
For me that means all the variable somehow belong to a class?
不,局部变量不是类的成员。
我住在黄色的房子里。里面的东西是不是也都是黄色的?
So How do they go to stack?
普通方法中的局部变量是短暂的,所以它进入堆栈。
只需停止将堆栈视为与存储的数据类型有关的想法。 堆栈与存储的数据类型无关。想想变量。栈是短期变量的地方。堆是存放长生命周期变量的地方。变量的类型无关紧要。
关于c# - 对象类原始类型堆栈和堆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21520774/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!