类,引用以节省内存

时间:2011-07-02 20:55:16

标签: vb.net

我有一个课程如下:

Public Class Courses
    Public CoursesOfferedMAIN As New List(Of Category)
    Public CoursesList As New List(Of Course)
    Public SemsList As New List(Of Sem)
    Public SubjectsList As New List(Of Subjects)
    Public ExamsTypeList As New List(Of ExamType)

    Public Class Category
        Private CategoryName As String
        Private Deleted As Boolean
        Public Courses As New List(Of Course)
    End Class
    Public Class Course
        Private CategoryName As String
        Private CourseID As String
        Private CourseName As String
        Private Deleted As Boolean
        Public Sems As New List(Of Sem)

    End Class
    Public Class Sem
        Private CategoryName As String
        Private CourseID As String
        Private SemID As String
        Private SemName As String
        Private Deleted As Boolean
        Public Subjects As New List(Of Subjects)
    End Class
    Public Class Subjects
        Private CategoryName As String
        Private CourseID As String
        Private SemID As String
        Private SubjectID As String
        Private SubjectName As String
        Private Deleted As Boolean
        Public Exams As New List(Of ExamType)
    End Class
    Public Class ExamType
        Private CategoryName As String
        Private CourseID As String
        Private SemID As String
        Private SubjectID As String
        Private ExamTypeID As String
        Private ExamName As String
        Private ExamMax As String
        Private ExamMin As String
        Private ExamPass As String
        Private Deleted As Boolean
    End Class

    Public Sub UpdateLists()
        CoursesList.Clear()
        SemsList.Clear()
        SubjectsList.Clear()
        ExamsTypeList.Clear()
        For Each Cat As Category In CoursesOfferedMAIN
            For Each cour As Course In Cat.Courses
                CoursesList.Add(cour)
                For Each sems As Sem In cour.Sems
                    SemsList.Add(sems)
                    For Each subj As Subjects In sems.Subjects
                        SubjectsList.Add(subj)
                        For Each exam As ExamType In subj.Exams
                            ExamsTypeList.Add(exam)
                        Next
                    Next
                Next
            Next
        Next
    End Sub
End Class

代码没有任何问题,我想知道,在我关注的过程中,科目,考试,考试等的数量都将在特定类型列表中重复,如SubjectsList,examstypelist等。因为它们被复制,所以需要更多的记忆。

因此,我的疑问或疑问是,是否可以使用相同的参考而不是将它们复制到列表以节省内存,或者是否有比这更好的方法?

1 个答案:

答案 0 :(得分:3)

您已使用参考资料。类是引用类型,因此当您复制对象时,实际上只是复制对象的引用。