对象库 - 访问索引值还是对象本身? (例如,深拷贝与浅拷贝也许?)
我一直对 .Net 如何复制引用感到困惑/不确定。 假设我有一个 GDI+ 位图对象。
dim foo as new bitmap("c:\foo.bmp")
'Foo' 保存位图对象。 现在假设我这样做。
dim bar as bitmap = foo
这是浅拷贝还是深拷贝?如果我将 foo 设置为无,那么 bar 是否也会突然引用“无”?或者 bar 是否也包含位图的副本,为了从内存中完全删除位图,我需要将 'foo' 和 'bar' 设置为空?
我需要在内存中保留一个位图库,对我来说,将每个创建的对象中每个位图的引用存储为变量会更容易,而不是使用索引对其进行编码并每次都必须引用该库它是需要的(例如“BitmapLibrary.Singleton.getBitmap(id)”)
简而言之,我可以这样做:
struct graphic object
dim myBitmap as bitmap
sub draw(g as graphics)
g.drawimage(myBitmap)
end sub
而不是这样:
struct graphic object
dim myBitmapIndex as integer
sub draw(g as graphics)
g.drawimage(bitmaplibrary.getImage(myBitmapIndex))
end sub
I've always been confused/unsure about how .Net copies references.
Let's say I have a Bitmap object for GDI+.
dim foo as new bitmap("c:\foo.bmp")
'Foo' holds the bitmap object.
Now let's say I do this.
dim bar as bitmap = foo
Is this a shallow copy or a deep copy? If I set foo equal to nothing, does bar suddenly reference 'nothing' as well? Or does bar contain a copy of the bitmap as well, and in order to remove the bitmap from memory completely, I need to set both 'foo' and 'bar' to nothing?
I need to keep a library of bitmaps in memory, and to me it would be easier to just store a reference to each bitmap in each created object as a variable, instead of coding it with an index and having to refer to the library each time it is needed (such as 'BitmapLibrary.Singleton.getBitmap(id)')
In a nutshell, can I do this:
struct graphic object
dim myBitmap as bitmap
sub draw(g as graphics)
g.drawimage(myBitmap)
end sub
instead of this:
struct graphic object
dim myBitmapIndex as integer
sub draw(g as graphics)
g.drawimage(bitmaplibrary.getImage(myBitmapIndex))
end sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,对象的引用只是复制引用。对于从 System.ValueType 继承的类型来说,情况并非如此,它是在 System.ValueType 中复制的。
例如:
对于 System.ValueType 后代:
如果我将
foo
设置为 Nothing,那么我只是将引用指向其他内容 (Nothing),并且没有更改bar 的对象参考文献。
如果要存储对象列表,将它们存储在列表结构中似乎是最谨慎的做法。但是,如果您知道您只会拥有固定的少量对象,那么将引用保留为类的字段可能是一个可行的选择。在您的情况下,您通过方法
getImage
抽象访问,这是一个很好的设计选择,因为这样您的类的用户就不必费心了解您跟踪引用的策略,并且您应该能够根据需要更改它,而不会破坏依赖于您的类的对象。In general, references to objects just copy the reference. This is not true for types which inherit from System.ValueType, where it is copied.
For example:
For System.ValueType descendants:
If I set
foo
to Nothing, then I've just made the reference point to something else (Nothing) and haven't changed the object whichbar
references.If you are storing a list of objects, storing them in a list structure seems most prudent. However, if you know that you are only going to ever have a fixed, small number of objects, then perhaps keeping the references as fields of a class is a viable option. In your case, you abstract access through the method
getImage
which is a good design choice, since then users of your class won't have to bother knowing your strategy for keeping track of references, and you should be able to change it as needed without breaking objects which depend on your class.