简体   繁体   中英

what can i use as a generic of List so i can change the type within the list in vb.net

so i currently have

class general
private list as List(Of string) 

public overridable function getprivateList as <not sure what to put>
    return list
end function
end class

class specific inherits general
private list as List(Of listofstrings)

end class

but I cannot figure out what to use as an interface . so that the method getprivateList is able to return both variations of list, depending on the class of implementation.

You could return IEnumerable , ICollection , or IList . IEnumerable has the least functionality and IList has the most.

Class General
    Private list As List(Of String) 

    Public Overridable Function GetPrivateList() As IList
        Return list
    End Function
End Class

Class Specific 
    Inherits General
    Private list As List(Of Integer)
End Class

Alternatively, you could define your base class as a generic class which takes the type of list item:

Class General(Of T)
    Private list As List(Of T) 

    Public Overridable Function GetPrivateList() As List(Of T)
        Return list
    End Function
End Class

Class Specific 
    Inherits General(Of Integer)
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM