设计问题:结构或类 [英] design question : struct or class

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

问题描述

你好,


我有一个设计问题。我需要跟踪一些变量,并且我计划将它们放在类或结构中。
。基本上我在谈论

10个bools,20个int和2个int数组。数组的大小将取决于某些外部值(从0到最大1000)的
。我会有一个

数组,其中最多255个这样的类/结构(在大多数情况下它会更少

然后是5)


由于没有真正的商业逻辑,我的选择将是结构。但是看看内存中对象大小的
,我认为它们有点大,可以放在堆栈上,而且应该更好。这意味着我应该使用课程。


关于什么是最好的想法?


TIA


Yves

Hello,

I''ve got a design question. I need to keep track of some variables and I am
planning to put them inside a class or struct. Basically I''m talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

Since there''s no real business logic my choice would be structs. But looking
at the size of the objects in memory I would think they are a bit large to
put on the stack and should be better of on the heap. Which would mean I
should use classes.

Any ideas about what would be best?

TIA

Yves

推荐答案

Phoenix,
因为没有真正的业务逻辑我的选择将是结构。
结构可以具有业务逻辑。和班级一样好。


我通常会把每件事都变成一个班级,除非我知道我需要一个结构,我使用

以下知道我需要一个结构:

- 表现得像一个原始类型

- 实例大小不超过16字节

- 是不可变的

- 值语义是可取的

http://msdn.microsoft.com/library/de...Guidelines.asp


希望这有帮助

Jay


" phoenix" < PA ****** @ skynetWORK.be>在留言中写道

新闻:O1 ************** @ TK2MSFTNGP12.phx.gbl ...你好,

我是'我有一个设计问题。我需要跟踪一些变量,并且我计划将它们放在一个类或结构中。基本上我说的是10个bool,20个int和2个int数组。数组的大小
取决于一些外部值(从0到最大1000)。我会有一个最多255个这些类/结构的数组(在大多数情况下它会更少
然后是5)

因为没有真正的业务逻辑我的选择将是结构。但是
查看内存中对象的大小我认为它们有点大,可以放在堆栈上,应该更好。这意味着我应该使用课程。

关于什么是最好的想法?

TIA

Yves
Since there''s no real business logic my choice would be structs. Structs can have "business logic" as equally well as classes.

I normally make every thing a class, unless I know I need a struct, I use
the following to know I need a struct:
- act like a primitive type
- have an instance size under 16 bytes
- are immutable
- value semantics are desirable

http://msdn.microsoft.com/library/de...Guidelines.asp

Hope this helps
Jay

"phoenix" <pa******@skynetWORK.be> wrote in message
news:O1**************@TK2MSFTNGP12.phx.gbl... Hello,

I''ve got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I''m talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

Since there''s no real business logic my choice would be structs. But looking at the size of the objects in memory I would think they are a bit large to
put on the stack and should be better of on the heap. Which would mean I
should use classes.

Any ideas about what would be best?

TIA

Yves



听起来你的对象有可能非常大。请记住

结构在堆栈上分配,这是有限的。我不知道

我的头顶堆栈大小。


" phoenix" < PA ****** @ skynetWORK.be>在消息中写道

新闻:O1 ************** @ TK2MSFTNGP12.phx.gbl ...
It sounds like your object has the potential of being quite large. Remember
that structures are allocated on the stack, which is finite. I don''t know
the stack size off the top of my head.

"phoenix" <pa******@skynetWORK.be> wrote in message
news:O1**************@TK2MSFTNGP12.phx.gbl...
你好,
我有一个设计问题。我需要跟踪一些变量,并且我计划将它们放在一个类或结构中。基本上我说的是10个bool,20个int和2个int数组。数组的大小
取决于一些外部值(从0到最大1000)。我会有一个最多255个这些类/结构的数组(在大多数情况下它会更少
然后是5)

因为没有真正的业务逻辑我的选择将是结构。但是
查看内存中对象的大小我认为它们有点大,可以放在堆栈上,应该更好。这意味着我应该使用课程。

关于什么是最好的想法?

TIA

Yves
Hello,

I''ve got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I''m talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

Since there''s no real business logic my choice would be structs. But looking at the size of the objects in memory I would think they are a bit large to
put on the stack and should be better of on the heap. Which would mean I
should use classes.

Any ideas about what would be best?

TIA

Yves



Pheonix ...要明确的是,如果你将结构添加到.NET _collection_,

他们

无论如何都会装箱。


问候,

Jeff
Pheonix... To be clear, if you add structures to a .NET _collection_,
they
will be boxed anyway.

Regards,
Jeff
这些类/结构中最多255个数组(在大多数情况下,它将
array of max 255 of these classes/structs (in most cases it will be



less

然后5))<

***通过Developersdex http://www.developersdex.com ***

Don''只是参加USENET ......获得奖励!


less
then 5 however)<
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


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

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