加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何将具有值类型和引用类型的对象存储在.NET中?

发布时间:2020-12-15 23:56:16 所属栏目:百科 来源:网络整理
导读:在.NET中,整数数据类型是值类型(堆栈),String是引用类型(堆). 因此,如果A类有一个整数,并且其中有一个字符串类型对象,并且B类创建了一个A类对象,那么A类的这个对象将如何存储在内存中?在堆栈中,还是在堆中? 这是在我的微软采访中提出的.需要了解我的表现.
在.NET中,整数数据类型是值类型(堆栈),String是引用类型(堆).

因此,如果A类有一个整数,并且其中有一个字符串类型对象,并且B类创建了一个A类对象,那么A类的这个对象将如何存储在内存中?在堆栈中,还是在堆中?

这是在我的微软采访中提出的.需要了解我的表现.

解决方法

Eric Lippert just wrote about this:

It is simply false that the choice of whether to use the stack or the heap has anything fundamentally to do with the type of the thing being stored.

真实的故事是:

“in the Microsoft implementation of C# on the desktop CLR,value types are stored on the stack when the value is a local variable or temporary that is not a closed-over local variable of a lambda or anonymous method,and the method body is not an iterator block,and the jitter chooses to not enregister the value.”

最重要的是,他强调你根本不应该关心某种类型的生活方式.你应该关心一定生活中的事物.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读