O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Use a Stack (LIFO Collection)

Problem

You need a collection where items will be retrieved in the reverse order (so that the most recently added item is always accessed first).

Solution

Use the System.Collections.Stack type, which provides a last-in, first-out collection.

Discussion

Stacks are used for programming tasks where you always need to access the most recent items first. Like the ArrayList and Queue, Stack is a dynamically sized array that stores objects. It starts with an initial capacity of 32 items (unless you supply a different capacity in the constructor) and doubles the capacity as needed. You can retrieve the number of items from the Count property, add an item at the top of a Stack with the Push method, or retrieve an item with the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required