List(Of T) tips for performance
I have been tempted to use List(Of T) in VB.NET instead of ArrayLists by someone-who-must-not-be-named-Robinson.
Essentially, what I need to store are three things: One string, two integers, and maybe two booleans. I liked using ArrayList, becau开发者_如何学Gose it treated all its elements as "Objects" so I didn't really have problems storing that variety of elements.
But now I have been thinking of using List(Of T). But how do I do to store that variety of elements of mine? I have been thinking on making a Structure
holding the five elements.
Sounds like a good idea? I heard somewhere that Structures are expected to have a short life. But I fear I will be having my structures for... forever, as long as my application lives. Does that make any difference?
Finally, what if I needed to create... don't know...... 125,000,000 Lists, each containing a structure? Is that possible (and good)?
If you're using a collection type (ArrayList
or List(Of T)
) just because you're looking for a place to store a fixed number of completely different types, stop! You're on the right track with creating a structure, though what you're looking for is called a class
.
Since, from your other questions, it sounds like you're creating a map editor, I'm assuming that your two numbers are X and Y coordinates, and I'd guess the string is a name of some kind? If so, very simple:
Public Class Tile
Public Property Name as String
Public Property X as Integer
Public Property Y as Integer
End Class
(Note that the above will only work if you're using Visual Studio 2010 and .NET 4; if not, respond and I'll edit with a pre-4 compatible version)
It will be much easier for you to refer to the .X
and .Y
properties rather than just throwing indexes around all over the place. This will also be much much faster (and take up less memory) than doing that.
I'm going to forego a complete description of everything that's there, as descriptions of what a class is and what properties are is pretty readily available. Does that help?
精彩评论