SoFunction
Updated on 2025-03-07

Detailed explanation of how to implement stack using Object class in C#

This article describes the method of C# using the Object class to implement the stack. Share it for your reference, as follows:

Stack class code:

using System;
using ;
using ;
using ;
namespace useObjectClass implementation of the first-out queue
{
 class Stack
 {
  private Object[] _items;
  public Object[] Items
  {
   get { return this._items; }
   set { this._items = value; }
  }
  //Press the object into  public void Push(Object obj)
  {
   //Initialize the first time when pressing, length is 1   if (this._items == null)
   {
    this._items = new 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 and out first  public Object Pop()
  {
   //When it is initialized or the length is 0, no elements cannot be taken out   if (this._items == null||this._items.Length == 0)
    return null;
   else
   {
    Object obj = this._items[this._items.Length - 1];
    //Delete the last element    ();
    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 ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace useObjectClass implementation of the first-out 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)
  {
   (this.);
  }
  private void button2_Click(object sender, EventArgs e)
  {
   Object[] objs = ;
   foreach(Object o in objs)
   {
    (());
   }
  }
  private void button1_Click_1(object sender, EventArgs e)
  {
   try
   {
    (().ToString());
   }
   catch
   {
    ("null");
   }
  }
  private void button3_Click(object sender, EventArgs e)
  {
   (this.);
  }
  private void button4_Click(object sender, EventArgs e)
  {
   try
   {
    (());
   }
   catch (InvalidOperationException)
   {
    ("null");
   }
  }
 }
}

1. When using Stack class, a lot of uncontrollable resource occupations are formed, waiting for GC to be recycled;

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

3. You can set an initial length of the Object array, without temporarily changing the length of the array every time you press in or take out. The specific method is to generate an array of specified length through the Stack constructor. When pressing in and taking out, the initial length is not adjusted, but just use an int value intPoint to record the position of the value currently owned. For the object that has been retrieved, it is not actually deleted, just ignore it. The advantage of this is that setting the array length at a time and positioning the "valid" element with something like a pointer is preferable.

In fact, the Stack<> generic class above provides a Stack<> that can be directly completed on the stack, which is very convenient to use, and avoids the losses caused by cast type conversion, achieving type safety. The second code has been given the usage method, which is very simple.

For more information about C# related content, please check out the topic of this site:C# data structure and algorithm tutorial》、《Summary of C# traversal algorithm and skills》、《Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.