本文介绍了值类型去哪儿了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我不知道在任何.NET论坛中已经问过多少次这个问题了,但是我仍然对值类型存储有一定的疑问.
几乎每本(我读过的)书都写到,值类型将存储在Stack中.但是在某些文章中,我读到值类型存储在声明的位置"是真的吗?
如果为true,则在类中声明的值类型将存储在Heap中.
GC将取消分配它们.
那么,每当我们在类
Box中使用值类型时,性能会如何?里面发生了什么?请我提供有关这些的更多信息,因为我找不到它.

Hi all,
I don''t know how many times this question has been asked in any .NET Forum, but still i have certain doubts regarding value types storage.
In almost every book (I read)it is written that value types are going to be stored in Stack. But in some articles i read "value types are stored where they are declared" is that true ?
If it is true then valu types declared in class will get stored in Heap.
And GC will deallocate them.
So what about the performance whenever we are using value types in class
Boxing is going to happen ? What happening inside ? Please i require more information regarding these as i couldn''t find it.

推荐答案

Sandeep Akhare写道:
Sandeep Akhare wrote:

如果如果为true,则在类中声明的值类型将存储在Heap中.

If it is true then valu types declared in class will get stored in Heap.



仅当它们所属类的对象分配在托管堆上.
如果它们是值类的成员怎么办?



Only if the object of the class they are members of is allocated on the managed heap.
What if they are members of a value class?

Sandeep Akhare写道:
Sandeep Akhare wrote:

GC将取消分配它们.

And GC will deallocate them.



GC仅管理在托管堆上分配的对象.



The GC only manages objects allocated on the managed heap.

Sandeep Akhare写道:
Sandeep Akhare wrote:

那么,每当我们在类中使用值类型时的性能如何?

So what about the performance whenever we are using value types in class



值类型通过值传递-这意味着每次传递值类型对象时都必须进行复制.这将比我猜想传递参考要慢.如果值类型是
ref类的成员,则没有区别-ref类对象保存值
type对象.

标记



Value types are passed by value - that means a copy has to be made
every time a value type object is passed. That''s going to be slower
than passing a reference I would guess. If value types are members of a
ref class then there''s no difference - the ref class object holds the value
type objects.

Mark


这篇关于值类型去哪儿了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 02:21