C # using the object class to implement the Stack method detailed _c# tutorial

Source: Internet
Author: User
Tags foreach

This example describes how C # uses the object class to implement stacks. Share to everyone for your reference, specific as follows:

Code for the Stack class:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
  Namespace uses the Object class to implement the LIFO queue {class Stack {private object[] _items;
   Public object[] The Items {get {return this._items}
  set {This._items = value;} The object is pressed into the public void Push (object obj) {//First pressed, initialized, length 1 if (This._items = null) {This._items = n
    EW object[1];
   This._items[0] = obj; else {int count = This._items.
    Length;
    object[] Objtemp = This._items;
    This._items = new Object[count + 1];
    int i = 0;
    foreach (Object o in objtemp) {this._items[i++] = O;
   } this._items[i] = obj; ///press back in first out to remove public Object Pop () {//for initialization or length 0 o'clock, cannot remove any elements if (This._items = = null| | This._items.
   Length = = 0) return null; else {Object obj = this._items[this._items.
    LENGTH-1]; Delete the last element this.
    Deletelastobj ();
   return obj; } private void Deletelastobj () {object[] objtemp = new ObjeCt[this._items.
   LENGTH-1]; for (int i = 0; i < This._items. Length-1;
   i++) {Objtemp[i] = this._items[i];
  } this._items = Objtemp;

 }
 }
}

Form detection code:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
  Namespace uses the object class to implement the LIFO queue {public partial class Form1:form {public Form1 () {InitializeComponent ();
  Private stack stack = new stack ();
  Private stack<string> stackgeneric= new stack<string> (); private void Button1_Click (object sender, EventArgs e) {stack.
  Push (This.textBox1.Text); private void Button2_Click (object sender, EventArgs e) {object[] Objs = stack.
   Items;
   foreach (Object o in Objs) {Console.WriteLine (o.tostring ()); }} private void Button1_click_1 (object sender, EventArgs e) {try {Console.WriteLine (This.stack.Pop ().
   ToString ());
   Catch {Console.WriteLine ("null");
  }} private void Button3_Click (object sender, EventArgs e) {This.stackGeneric.Push (this.textBox2.Text); } private void BuTton4_click (object sender, EventArgs e) {try {Console.WriteLine (This.stackGeneric.Pop ());
   catch (InvalidOperationException) {Console.WriteLine ("null");

 }
  }
 }
}

1. The use of stack class when the formation of a lot of uncontrolled resource consumption, waiting for GC recovery;

2. Type is unsafe, any type of data can be loaded into object

3. You can set an initial length of an object array, instead of having to temporarily change the length of the array every time you press in or out, you do this by generating an array of a specified length through the stack's constructor, which does not adjust the length of the initialization when pressed in and out. Instead of using an int value intpoint to record the position of the current value, the object that has been taken is not actually deleted, just leave it alone. The advantage of this is that it is preferable to set the length of an array at once and position the "valid" element with something like a pointer.

In fact, the. net2.0 above provides a stack<> generic class that can directly complete the stack, is very convenient to use, and avoids the loss caused by coercion of type conversions and implements type safety. The second section of the code has been given a way to use, very simple.

For more information on C # related content readers can view the site topics: "C # Data structure and algorithm tutorial", "C # traversal algorithm and Skills summary", "C # Design Thread Usage Tips summary", "C # Operation Excel Skills Summary", "C # XML file Operation skills Summary", "C # Common control usage tutorials, summary of WinForm control usage, summary of C # array operations tips, and the introductory course on C # object-oriented programming

I hope this article will help you with C # programming.

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.