结构与类对象 [英] Structure vs Class Objects

查看:57
本文介绍了结构与类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有类似的结构;


公共结构myStructureDef

Public b()as Byte

Public t as字符串

结束结构


如果我通过这个结构,数组b中的值是否会存储在

堆栈中或只是一个指向数组的指针存储在堆栈中?我是

试图决定是否使用Structures或Pointers。我知道M''soft

建议使用一个类,如果长度超过大约16个字节,但这样做

包括所有数组元素或只是指向数组的指针?

-

Dennis在休斯顿

If I have a structure like;

Public Structure myStructureDef
Public b() as Byte
Public t as String
End Structure

If I pass this structure, will the values in the array b be stored on the
stack or will just a pointer to the array be stored on the stack? I am
trying to decide whether to use Structures or Pointers. I know that M''soft
recommends to use a class if the length is over about 16 bytes but does this
include all the array elements or just pointers to the array?
--
Dennis in Houston

推荐答案

数组和字符串都是引用类型,所以他们将被分配到

GC堆上。我认为16字节规则只是一个指导,旨在让你考虑是否真的需要值类型或引用类型语义。

例如。如果你将myStructureDef传递给一个方法,你将传递一个副本,

包括一个数组和字符串的副本。如果您将一个实例分配给

另一个实例,那么您将再分配一个副本而不是两个实例

引用相同的数据。如果你发现自己传递myStructureDef

参数ByRef很多,那么你应该考虑使用引用类型

。您还应该考虑在您的核心场景中myStructureDef的装箱频率




-

Kevin Westhead


" Dennis" <德**** @ discussions.microsoft.com>在消息中写道

news:15 ********************************** @ microsof t.com ...
Arrays and strings are both reference types, so they''ll be allocated on the
GC heap. I think the 16 byte rule is just one guideline aimed at getting you
to consider whether you really want value type or reference type semantics.
E.g. if you pass myStructureDef to a method, you''ll be passing a copy, which
includes a copy of the array and the string. If you assign one instance to
another, again you''ll be assigning a copy rather than having both instances
reference the same data. If you find yourself passing myStructureDef
arguments ByRef alot then you should really consider using a reference type
instead. You should also think about how often myStructureDef will be boxed
in your core scenarios.

--
Kevin Westhead

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
如果我有类似的结构;

公共结构myStructureDef
Public b()as Byte
Public t as String
结束结构

如果我通过这个结构,数组b中的值是否会存储在
堆栈中,或者只是指向数组的指针存储在堆栈中?我正在尝试决定是否使用Structures或Pointers。我知道,如果长度大约超过16个字节,那么M''soft
建议使用一个类,但是
这包括所有数组元素或只是指向数组的指针?
-
Dennis in Houston
If I have a structure like;

Public Structure myStructureDef
Public b() as Byte
Public t as String
End Structure

If I pass this structure, will the values in the array b be stored on the
stack or will just a pointer to the array be stored on the stack? I am
trying to decide whether to use Structures or Pointers. I know that
M''soft
recommends to use a class if the length is over about 16 bytes but does
this
include all the array elements or just pointers to the array?
--
Dennis in Houston



一个合理的回应。


- -

(OHM) - 一名男子

AKA Terry Burns - http://TrainingOn.net


" Kevin Westhead" <毫安*********** @ nospam.nospam>在消息中写道

news:uz ************** @ TK2MSFTNGP15.phx.gbl ...
A reasoned response.

--
( OHM ) - One Handed Man
AKA Terry Burns - http://TrainingOn.net

"Kevin Westhead" <ma***********@nospam.nospam> wrote in message
news:uz**************@TK2MSFTNGP15.phx.gbl...
数组和字符串都是引用类型,因此它们将被分配在GC堆上。我认为16字节规则只是一个指南,旨在让您考虑是否真的需要值类型或引用类型语义。例如。如果你将myStructureDef传递给一个方法,你将传递一个副本,其中包括数组的副本和字符串。如果您将一个实例分配给另一个实例,那么您将再次指定一个副本,而不是让两个实例都引用相同的数据。如果你发现自己传递myStructureDef参数ByRef很多,那么你应该考虑使用引用类型。您还应该考虑如何在您的核心场景中经常使用myStructureDef。

- Kevin Westhead

Dennis <德**** @ discussions.microsoft.com>在消息中写道
新闻:15 ********************************** @ microsof t.com。 ..
Arrays and strings are both reference types, so they''ll be allocated on
the GC heap. I think the 16 byte rule is just one guideline aimed at
getting you to consider whether you really want value type or reference
type semantics. E.g. if you pass myStructureDef to a method, you''ll be
passing a copy, which includes a copy of the array and the string. If you
assign one instance to another, again you''ll be assigning a copy rather
than having both instances reference the same data. If you find yourself
passing myStructureDef arguments ByRef alot then you should really
consider using a reference type instead. You should also think about how
often myStructureDef will be boxed in your core scenarios.

--
Kevin Westhead

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
如果我有类似的结构;

公共结构myStructureDef
Public b()as Byte
Public t as String
结束结构

如果我通过这个结构,数组b中的值是否会存储在
堆栈中,或者只是指向数组的指针存储在堆栈中?我正在尝试决定是否使用Structures或Pointers。我知道,如果长度大约超过16个字节,那么M''soft
建议使用一个类,但是
这包括所有数组元素或只是指向数组的指针?
-
Dennis in Houston
If I have a structure like;

Public Structure myStructureDef
Public b() as Byte
Public t as String
End Structure

If I pass this structure, will the values in the array b be stored on the
stack or will just a pointer to the array be stored on the stack? I am
trying to decide whether to use Structures or Pointers. I know that
M''soft
recommends to use a class if the length is over about 16 bytes but does
this
include all the array elements or just pointers to the array?
--
Dennis in Houston




感谢您的澄清。由于我使用的是非常大的数组,我认为我会使用Classses来避免占用内存,即使我必须在我的应用程序中进行多次更改



-

丹尼斯在休斯敦

" Kevin Westhead"写道:
Thanks for your clarification. Since I''m using very large arrays, I think I
will use Classses to avoid eating up memory even though I will have to make
several changes in my application.
--
Dennis in Houston
"Kevin Westhead" wrote:
数组和字符串都是引用类型,因此它们将被分配在GC堆上。我认为16字节规则只是一个指南,旨在让您考虑是否真的需要值类型或引用类型语义。
例如如果你将myStructureDef传递给一个方法,你将传递一个副本,它包含一个数组和字符串的副本。如果您将一个实例分配给另一个实例,那么您将再次分配一个副本,而不是让两个实例都引用相同的数据。如果您发现自己传递了myStructureDef
参数ByRef,那么您应该考虑使用引用类型
。您还应该考虑在您的核心场景中myStructureDef的装箱频率。

-
Kevin Westhead

Dennis <德**** @ discussions.microsoft.com>在消息中写道
新闻:15 ********************************** @ microsof t.com。 ..
Arrays and strings are both reference types, so they''ll be allocated on the
GC heap. I think the 16 byte rule is just one guideline aimed at getting you
to consider whether you really want value type or reference type semantics.
E.g. if you pass myStructureDef to a method, you''ll be passing a copy, which
includes a copy of the array and the string. If you assign one instance to
another, again you''ll be assigning a copy rather than having both instances
reference the same data. If you find yourself passing myStructureDef
arguments ByRef alot then you should really consider using a reference type
instead. You should also think about how often myStructureDef will be boxed
in your core scenarios.

--
Kevin Westhead

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
如果我有类似的结构;

公共结构myStructureDef
Public b()as Byte
Public t as String
结束结构

如果我通过这个结构,数组b中的值是否会存储在
堆栈中,或者只是指向数组的指针存储在堆栈中?我正在尝试决定是否使用Structures或Pointers。我知道,如果长度大约超过16个字节,那么M''soft
建议使用一个类,但是
这包括所有数组元素或只是指向数组的指针?
-
Dennis in Houston
If I have a structure like;

Public Structure myStructureDef
Public b() as Byte
Public t as String
End Structure

If I pass this structure, will the values in the array b be stored on the
stack or will just a pointer to the array be stored on the stack? I am
trying to decide whether to use Structures or Pointers. I know that
M''soft
recommends to use a class if the length is over about 16 bytes but does
this
include all the array elements or just pointers to the array?
--
Dennis in Houston




这篇关于结构与类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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