结构与类 [英] Struct vs. Class

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

问题描述

如果我打算在

表中创建封装数据行的对象,那么使用Struct或Class会更好吗?根据我读到的内容,如果我的

对象只需要获取和设置方法,那么Struct可能会更好......但是我想要b $ b我正在寻找一些建议来自这方面的专家?

假设:有可能进行一些操作,我可能会有8000到10000个b $ b(或更多)对象被实例化。


设计一个可以实例化这个

许多对象的系统有哪些考虑因素?结构会给我什么样的性能优势

类?


谢谢

If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.

what are some considerations in designing a system that may instantiate this
many objects? what sort of performance advantage would Structs give me over
Classes?

thank you

推荐答案

dimension写道:
dimension wrote:
如果我的目的是创建在表格中封装数据行的对象,那么使用Struct或Class是否更好?根据我读到的内容,如果我的
对象只需要获取和设置方法,那么Struct可能会更好......但是我正在寻找专家的一些建议吗?
假设:对于某些操作是可能的,我可能有8000到10000个(或更多)对象被实例化。


仅对复杂值类型使用结构,例如具有特殊

特征的int。结构是值类型,而不是对象,所以只要你将它们用作对象,你就会遇到问题。例如,如果

你索引到一个arraylist并执行此操作:

((MyStructType)myArrayList [index])。Property = value;


如果在某个位置找到一个结构,索引器将返回一个副本,因为它是一个值类型(如果我没记错的话,C#甚至不会编译

以上)。


所以经验法则:几乎总是使用类,而不是结构。

设计系统时需要考虑的因素那可能会实例化很多物体?结构会给我什么样的性能优势
类?
If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.
ONLY use structs for complex value types, like an int with special
characteristics. Structs are value types, not objects, so as soon as
you''re using them as objects, you''ll run into issues. For example, if
you index into an arraylist and do this:
((MyStructType)myArrayList[index]).Property = value;

if at that spot a struct is located, the indexer will return a copy, as
it is a value type (If I recall correctly, C# will not even compile the
above line).

So rule of thumb: almost always use classes, not structs.
what are some considerations in designing a system that may instantiate this
many objects? what sort of performance advantage would Structs give me over
Classes?




无。


FB


-

获取LLBLGen Pro,用于.NET的高效O / R映射: http://www.llblgen.com

我的.NET博客: http://weblogs.asp.net/FBouma

Microsoft MVP(C#)



none.

FB

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog : http://weblogs.asp.net/FBouma
Microsoft MVP (C#)


如果您打算通过每个

行的对象提供对返回数据的访问权限,那么最好只创建实际需要的对象
$ b $时间。您可以将数据集保存在内存中,并且可以调用一些工厂类

,这将为特定的

行创建并返回一个对象。


" dimension" <二******* @ discussions.microsoft.com>在消息中写道

新闻:FA ********************************** @ microsof t.com ...
If you plan to provide access to the returned data via an object for each
row, it might be better to only create objects that are actually needed at
the time. You could hold the dataset in memory and have some factory class
that you can call which will create and return an object for a particular
row.

"dimension" <di*******@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
如果我的目的是创建将数据行封装在
表中的对象,那么使用Struct或Class是否更好?根据我读到的内容,如果我的
对象只是获取和设置方法,那么Struct可能会更好......但我正在寻找专家的一些建议吗?
假设:对于某些操作是可能的,我可能有8000到10000个(或更多)对象被实例化。

在设计可能实例化的系统时有哪些考虑因素<这个很多物体?结构会给我什么样的性能优势?

谢谢
If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be
better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.

what are some considerations in designing a system that may instantiate
this
many objects? what sort of performance advantage would Structs give me
over
Classes?

thank you



参见
http://msdn.microsoft.com/library/de...guidelines.asp

相反前面的评论,结构可以提供性能增强

*如果使用正确*。它们被分配在堆栈上,而不是堆,因此不需要由GC管理



性能提升实际上取决于数据的方式将被使用。


希望这会有所帮助

Dan


" dimension"写道:
See
http://msdn.microsoft.com/library/de...guidelines.asp

Contrary top previous comments, structs can provide performance enhancements
*if used correctly*. They are allocated on the stack, not heap, and so do not
need to be managed by the GC.

Performance gains really depend on how the data will be used.

Hope this helps
Dan

"dimension" wrote:
如果我的意图是创建在表格中封装数据行的对象,那么使用Struct或Class是否更好?根据我读到的内容,如果我的
对象只需要获取和设置方法,那么Struct可能会更好......但是我正在寻找专家的一些建议吗?
假设:对于某些操作是可能的,我可能有8000到10000个(或更多)对象实例化。

在设计可能实例化的系统时有哪些考虑因素
很多物品? Structs会给我什么样的性能优势?

谢谢你
If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.

what are some considerations in designing a system that may instantiate this
many objects? what sort of performance advantage would Structs give me over
Classes?

thank you



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

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