C # stack of collection,

Source: Internet
Author: User

C # stack of collection,

Stack is a very similar container to queue, But Stack is a post-in-first-out (LIFO) container.
The stack uses the Push () method to add elements to the stack, and uses the Pop () method to obtain the most recently added element:

Similar to the Queue <T> class (http://www.cnblogs.com/afei-24/p/6829817.html), Stack implements the ICollection and IEnumerable <T> interfaces.
Stack <T> class member:

In a foreach statement, the stack enumerator does not delete elements, but returns elements one by one. Using the Pop () method, each element is read from the stack and deleted.

Static void Main () {var alphabet = new Stack <char> (); alphabet. push ('A'); alphabet. push ('B'); alphabet. push ('C'); Console. write ("First iteration:"); foreach (char item in alphabet) {Console. write (item);} Console. writeLine (); Console. write ("Second iteration:"); while (alphabet. count> 0) {Console. write (alphabet. pop ();} Console. writeLine ();}

The stack becomes empty after the second iteration.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.