Stack of C # collections

Source: Internet
Author: User

Reprint: http://www.cnblogs.com/wisdomforce/archive/2010/05/23/1742098.htmlC# set of STACK1, stack definition

The System.Collections.Stack class represents a simple LIFO non-generic collection of objects.

2. Advantages

1, the collection of LIFO.

2. Stack can accept null references as valid values and allow duplicate elements

3. Stack's constructor

Constructor functions

Comments

Stack ()

Initializes a new instance of the Stack class that is empty and has the default initial capacity.

Stack (ICollection)

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

Stack (Int32)

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

4. Stack Properties

Property name

Comments

Count

Gets the number of elements contained in the Stack.

5. Stack method

Method name

Comments

Void Clear ()

removes all objects from the Stack.

Bool Contains (object obj)

Determines whether an element is in the Stack.

Object Clone ()

Creates a shallow copy of the Stack.

Void CopyTo (Array array,int index)

Copies the Stack to an existing one-dimensional array starting at the specified array index .

Object Pop ()

Removes and returns the object at the top of the Stack.

Void Push (object obj)

Inserts an object into the top of the Stack.

Object Peek ()

Returns the object at the top of the Stack without removing it.

Object[]toarray ()

Copy the Stack to the new array.

6. Example of stack usage

1 class Program2     {3          Public Static voidMain ()4         {5 6             //Create a stack7Stack Mystack =NewStack ();8Mystack.push (" the");//into the stack9Mystack.push ("Quick");TenMystack.push ("Brown"); OneMystack.push ("Fox"); A              -  -             //print values from a collection theConsole.Write ("Stack values:"); -Printvalues (Mystack,'\ t'); -  -             //prints the first element at the top of the stack and removes it +Console.WriteLine ("(POP) \t\t{0}", Mystack.pop ()); -  +             //print values from a collection AConsole.Write ("Stack values:"); atPrintvalues (Mystack,'\ t'); -  -             //prints the first element at the top of the stack and removes it -Console.WriteLine ("(POP) \t\t{0}", Mystack.pop ()); -  -             //print values from a collection inConsole.Write ("Stack values:"); -Printvalues (Mystack,'\ t'); to  +             //the first element at the top of the print stack -Console.WriteLine ("(Peek) \t\t{0}", Mystack.peek ()); the  *             //print values from a collection $Console.Write ("Stack values:");Panax NotoginsengPrintvalues (Mystack,'\ t'); - Console.read (); the         } +  A  the          Public Static voidPrintvalues (IEnumerable mycollection,Charmyseparator) +         { -             foreach(Object objinchmycollection) $Console.Write ("{0}{1}", Myseparator, obj); $ Console.WriteLine (); -         } -  the}
View Code

7. Notes

1. Stack capacity is the number of elements that stack can hold. The default initial capacity of the Stack is 10. When you add elements to a Stack, the capacity is automatically increased as needed by reallocation.

2, the capacity is the number of elements that Stack can store. Count is the number of elements actually stored in the Stack, and the capacity is always greater than or equal to count. If Count exceeds capacity when adding elements, the capacity is automatically increased by reallocating the internal array before copying the old elements and adding new elements

Stack of C # collections

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.