A list with different types of objects?

Can I have a list containing one line and two numbers? Or can I only have one type of item?

+3
source share
4 answers

If you need this kind of functionality, I would look at a non-generic type System.Collections.ArrayList.

Update

For those of you who are not going to read a huge chain of comments ... it looks like Adam Robinson is using something List<object>over ArrayList. Both will work, but in large collections it seems to be List<object>measurably faster than that ArrayList.

+8
source

You can. A list of objects can do this. But you are losing type safety with this, as well as intelliSense development time.

? 3 .

+4

No, containers such as List(Of T)only store one type of item T. You can, however, make this one type a single line and two numbers.

Structure Foo
    Public Desc As String
    Public x As Integer, y As Integer
End Structure

Dim List = New List(Of Foo)
+3
source

Yes, you can.

dim myVehicles as new list(of object)
dim myCar as new car
dim myBike as new bike
dim mySecondCar as new car


myVehicles.add(myCar)
myVehicles.add(myBike)
myVehicles.add(mySecondCar)
0
source

Source: https://habr.com/ru/post/1788584/


All Articles