是否应手动处理小型定制结构? [英] Should a small custom structure be manually disposed?

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

问题描述

我有这个小结构,我只用来管理一些组合框的内容而不是数据库,通过读取XML文件,但每次更改另一个组合框的索引时,整个组合框的内容都被清除并且重新填充,显然有新的结构对象。



这样做,我知道我从组合框项目列表中删除了所有结构,但是没有丢弃对象,据我所知。



我创建了一个结构,因为我读到一个结构很好,当你要实例化太多的小类时,因为它们是在内存中堆叠得更快...

无论如何我对结构不是很熟悉,而且我正在尝试新知识,所以我想知道这两件事:



我应该使结构是一次性的还是GC自动完成的,还有;这是一个很好的结构,或者更好地使它成为一个类?



I have this small structure that i use only to manage the contents of some comboboxes instead of a database, by reading from an XML file, but each time the index of another combobox is changed, the contents of the whole combobox gets cleared and refilled, obviously with new structure objects.

At doing this, i know i am removing all the structures from the comboboxes item's list, but the objects are not disposed, as far as i know.

I've created an structure because I read that a structure is good when you are going to instantiate too many small classes, because they are faster at stacking in memory...
In any case im not very familiarized with the structures and also i was trying the new knowledge, so i want to know both things:

Should I make the structure disposable or the GC does it automatically, and also; is this a good use for a structure, or is better to make it a class?

Friend Structure FileRef

    Private _FileName, _Extension, _Path As String

    Public Property Display As String

    Public ReadOnly Property Path As String
        Get
            Return Me._Path
        End Get
    End Property
    Public ReadOnly Property FileName As String
        Get

            Return Me._FileName
        End Get
    End Property
    Public ReadOnly Property Extension As String
        Get


            Return Me._Extension
        End Get
    End Property

    Public Sub New(ByVal path As String)

        Me._Path = path
        Me._Extension = System.IO.Path.GetExtension(Me._Path)
        Me._FileName = System.IO.Path.GetFileName(Me._Path)
    End Sub
    Public Sub New(ByVal path As String, ByVal AutoDisplay As Boolean)

        Me._Path = path
        Me._Extension = System.IO.Path.GetExtension(Me._Path)
        Me._FileName = System.IO.Path.GetFileName(Me._Path)
        If AutoDisplay = True Then _
          Me.Display = System.IO.Path.GetFileNameWithoutExtension(Me.Path)
    End Sub
    Public Sub New(ByVal path As String, ByVal display As String)

        Me._Path = path
        Me._Extension = System.IO.Path.GetExtension(Me._Path)
        Me._FileName = System.IO.Path.GetFileName(Me._Path)
        Me.Display = display
    End Sub

    Public Overrides Function ToString() As String

        If Me.Display = Nothing Then Return Me.Path Else Return Me.Display
    End Function

End Structure





我这样使用它:





I use it like this:

Private Sub cboCollection_SelectedIndexChanged(sender As Object, e As EventArgs) _
                                    Handles cboCollection.SelectedIndexChanged

        If cboCollection.SelectedIndex >= 0 Then

            Dim SelItem As FileRef = DirectCast _
                                    (cboCollection.SelectedItem, FileRef)

            cboCollection.Items.Clear()
            cboImage.SelectedIndex = -1

            Using fs As New FileStream(SelItem.Path,_
                          FileMode.Open, FileAccess.Read, FileShare.Read)
                Using xmlR As XmlReader = XmlReader.Create(fs)
                   
                    Try 

                    'Code that reads the xml 
                    'and creates in iteration the FileRef objects
                    'to then add them 1 by 1 to another combobox

                    Catch ex As Exception

                    'Error management
                    End Try
                End Using
            End Using
        End If
End Sub





如果我的英语不好,请提前感谢对不起。



PS。我在VisualBasic中写过,但C#中的答案也非常受欢迎。



Thanks in advance and sorry if i had bad english.

PS. I wrote in VisualBasic but answers in C# are also very welcome.

推荐答案

首先,您应该了解GC操作(完成和回收托管内存)之间的区别和定稿。 Disposal( System.IDisposable )是一个单独的机制,可用于清理所有内容。它通常用于处理非托管资源,但它不是唯一的目的。基本上,如果你不使用非托管内存或其他资源而且你不知道还需要哪些处理,那么你可能根本不需要处理。



对于结构与类,解决方案1非常不准确。 经过讨论后,其作者已将其删除。 [结束编辑]



您可以使用结构而不是类轻松地破坏性能,并且总是可以采用相反的情况。此外,您应该了解引用类型的成员可以是值类型,结构成员(值类型)可以是引用类型。 选择哪一个的问题完全是错误的。您需要深入了解参考和价值类型,而不是询问这些问题。具体的决定取决于许多因素,从您的目标开始。根据我的经验,人们通常通过将结构更改为类来解决结构问题,但这与性能无关,并且绝不意味着您应该忽略使用结构的可能性。你应该清楚地知道什么是什么。如果你有一些特别的担忧,我很乐意回答。



-SA
First of all, you should understand the difference between GC action (finalization and reclaiming managed memory) and finalization. Disposal (System.IDisposable) is a separate mechanism which can be used to clean-up everything. It is often used to dispose unmanaged resource, but it is not the only purpose. Basically, if you don't use unmanaged memory or other resources and you don't know what else the disposal is needed for, chance are, you don't need disposal at all.

As to structures vs classes, Solution 1 is quite inaccurate. After out discussion, its author has removed it. [END EDIT]

You can easily compromise performance using structures instead of classes, and the opposite situation is always possible. Also, you should understand that members of reference types can be of value types, and member of structures (value types) can be of reference types. The question "which one to choose" is simply wrong. Instead of asking such questions, you need to get in-depth understanding of reference and value types. The particular decision depends on many factors, starting from your goals. In my experience, people usually screw up things with structures the way it could be fixed by changing a structure to a class, but this is not related to performance and absolutely does not mean that you should ever ignore the possibility of using structures. You should clearly understand what does what. If you had some particular concerns about it, I would gladly answer.

—SA


这篇关于是否应手动处理小型定制结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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