Stack represents aLater in and first outcollection of objects. When you need to make last-in first-out access to each item, use the stack. When you add an item to the list, it is calledPush inElement, when you remove an item from a list, is calledpop upelement.
So under what circumstances should I use stack? See an example:
When the bullet is destroyed, put the bullet into the stack. When there are bullets in the stack, it is created when there are bullets in the stack.
private static Stack<Bullet> bulletStack = new Stack<Bullet>(); public static Bullet CreateBullet() { Bullet bullet; if (>0) { bullet = (); } else { bullet = new Bullet(); } return bullet; } public static void Destroy(Bullet bullet) { (bullet); }
Stack class methods and properties
StackSome commonly used classesproperty:
property | describe |
---|---|
Count | Gets the number of elements contained in Stack. |
StackSome commonly used classesmethod:
Serial number | Method Name & Description |
---|---|
1 | public virtual void Clear(); Remove all elements from Stack. |
2 | public virtual bool Contains( object obj ); Determines whether an element is in Stack. |
3 | public virtual object Peek();Returns the object at the top of Stack, but does not remove it. |
4 | public virtual object Pop();Removes and returns the object at the top of Stack. |
5 | public virtual void Push( object obj );Add an object to the top of Stack. |
6 | public virtual object[] ToArray();Copy Stack into a new array. |
This is the end of this article about how to use C# Stack Stack. For more related contents of C# Stack, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!