VB.NET:使用结构是否令人讨厌? [英] VB.NET: is using Structures considered nasty?
问题描述
我在 VB6 时代经常使用 Structures,而现在在 .NET 中尽量避免使用它们.只是想知道在 2010 年使用结构而不是类是否被认为是令人讨厌的?
I use to use Structures quite a lot in the VB6 days, and try to avoid them now with .NET. Just wondering if using structures in 2010 instead of a Class is considered nasty?
感谢您的帮助.
推荐答案
选择 Structure
需要考虑,而不是天生就讨厌".结构可能令人讨厌是有原因的;但是也有一些原因 Class
可能会以自己的方式令人讨厌......
Choosing a Structure
takes consideration instead of being inherently "nasty". There are reasons why a Structure can be nasty; however there are also reasons a Class
can be nasty in its own way...
基本上,当您在这两种面向对象类型的容器之间做出选择时,您就是在决定如何使用内存.
Basically when you decide between these two object oriented kinds of containers, you're deciding how memory will be used.
在 VB.NET 中,Structure
和 Class
有不同的语义,它们代表不同的内存使用模式.
There are different semantics associated with Structure
and Class
in VB.NET and they represent different memory usage patterns.
通过创建一个Class
,您正在创建一个引用类型.
By creating a Class
you're creating a reference type.
- 适合大数据
- 内存包含对堆上对象位置的引用(就像指向对象的概念一样),但对 VB.NET 程序员来说是透明的,因为您处于托管模式".
通过创建Structure
,您正在创建值类型.
By creating a Structure
you're creating a value type.
- 适合小数据
- 分配的内存包含实际值
- 要明智,因为这些很容易被推到内存的堆栈区域(即对于本地变量,而不是类字段) - 太大,你可能会遇到堆栈问题.
YouTube 上还有一些不错的视频资源,如果你是一名音频学习者.
Also some good video resources on YouTube if you're an audio learner.
互联网上的许多文章都喜欢这些 MSDN 文章来教授基础知识和细节:
Many articles on the Internet like these MSDN articles to teach the basics and details:
示例
这篇关于VB.NET:使用结构是否令人讨厌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!