Stack and C# examples

WBOY
Release: 2023-09-20 22:45:05
forward
985 people have browsed it

堆栈与 C# 示例

The Stack class in C# represents a simple last-in-first-out (LIFO) non-generic collection of objects.

The following are the properties of the Stack class-

Sr.NoProperties & Description
1Count p>

Get the number of elements contained in the Stack.

2IsSynchronized

Gets a value indicating whether to access the stack Synchronous (thread-safe).

3SyncRoot

Get objects available for synchronous access

The following are some methods of Stack class-

##1234567##8GetType() 9Peek()10Pop()11Push(Object)Examples
Sr.No Properties and Description th>
Clear()Remove from the stack All objects.

Clone()Creates a shallow copy of the stack.

Contains(Object) Whether the element is on the stack.

CopyTo(Array, Int32)Copy Convert Stack to existing a one-dimensional array of Start at the specified array index.

Equal to (Object)Determine whether the specified object is equal to current object.

GetEnumerator() strong>Returns the IEnumerator of the stack.

td>GetHashCode() Used as the default hash function. (Inherited from Object)

Get the Type of the current instance .

Returns the object at the top of the stack without deleting it.

Deletes and returns the object at the top of the stack

Insert an object at the top of the stack.

Now let us see some examples-

To get the object on top of the stack, the code is as follows -

Live Demonstration

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push("A");
      stack.Push("B");
      stack.Push("C");
      stack.Push("D");
      stack.Push("E");
      stack.Push("F");
      stack.Push("G");
      stack.Push("H");
      stack.Push("I");
      stack.Push("J");
      Console.WriteLine("Count of elements = "+stack.Count);
      Console.WriteLine("Element at the top of stack = " + stack.Peek());
   }
}
Copy after login

Output

This will produce the following output-

Count of elements = 10
Element at the top of stack = J
Count of elements = 10
Copy after login

To check if the Stack has elements, use the C# Contains() method . Below is the code -

Example

Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push(100);
      stack.Push(150);
      stack.Push(175);
      stack.Push(200);
      stack.Push(225);
      stack.Push(250);
      stack.Push(300);
      stack.Push(400);
      stack.Push(450);
      stack.Push(500);
      Console.WriteLine("Elements in the Stack:");      
      foreach(var val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements in the Stack = "+stack.Count);
      Console.WriteLine("Does Stack has the element 400?= "+stack.Contains(400));
   }
}
Copy after login

Output

This will produce the following output -

Elements in the Stack:
500
450
400
300
250
225
200
175
150
100
Count of elements in the Stack = 10
Does Stack has the element40400?= False
Copy after login

The above is the detailed content of Stack and C# examples. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!