C # Stack of a set

Source: Internet
Author: User

1. Stack Definition

The System. Collections. Stack class indicates a simple post-first-out non-generic set of objects.

2. Advantages

1. A collection of later, first, and foremost.

2. Stack can accept null references as valid values and allow repeated Elements

3. Stack Constructor

Constructors

Note

Stack ()

Initialize a new instance of the Stack class. The instance is empty and has the default initial capacity.

Stack (ICollection)

Initializes a new instance of the Stack class. The instance contains elements copied from the specified set and has the same initial capacity as the number of copied elements.

Stack (Int32)

Initialize a new instance of the Stack class. The instance is empty and has the specified initial capacity or default initial capacity (larger of the two capacities ).

4. Stack attributes

Attribute name

Note

Count

Obtains the number of elements contained in a Stack.

5. Stack Method

Method Name

Note

Void Clear ()

Remove all objects from the Stack.

Bool Contains (object obj)

Determine whether an element is in the Stack.

Object Clone ()

Create a superficial copy of the Stack.

Void CopyTo (Array array, int index)

Copy the Stack from the specified Array index to an existing one-dimensional Array.

Object Pop ()

Remove and return the object at the top of the Stack.

Void Push (object obj)

Insert the object to the top of the Stack.

Object Peek ()

Returns the object at the top of the Stack but does not remove it.

Object [] ToArray ()

Copy the Stack to the new array.

 

6. Example of Stack usage

 

Code
 Class Program
{
Public static void Main ()
{

// Create a stack
Stack myStack = new Stack ();
MyStack. Push ("The"); // inbound Stack
MyStack. Push ("quick ");
MyStack. Push ("brown ");
MyStack. Push ("fox ");


// Print the value in the Set
Console. Write ("Stack values :");
PrintValues (myStack,
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.