Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 Stack.Peek() 메서드

<시간/>

C#의 Stack.Peek() 메서드는 스택의 맨 위에 있는 개체를 제거하지 않고 반환하는 데 사용됩니다.

구문

구문은 다음과 같습니다 -

public virtual object Peek ();

예시

이제 예를 살펴보겠습니다 -

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push("Inspiron");
      stack.Push("Alienware");
      stack.Push("Projectors");
      stack.Push("Monitors");
      stack.Push("XPS");
      stack.Push("Laptop");
      stack.Push("Notebook");
      Console.WriteLine("Stack elements...");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements = "+stack.Count);
      Console.WriteLine("Element at the top = "+ stack.Peek());
      stack.Push("Ultrabook");
      stack.Push("Cameras");
      stack.Push("Keyboards");
      Console.WriteLine("\nStack elements...updated");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Element at the top = "+ stack.Peek());
      Console.WriteLine("\nCount of elements (updated) = "+stack.Count);
      stack.Clear();
      Console.Write("Count of elements (updated) = "+stack.Count);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Stack elements...
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Count of elements = 7
Element at the top = Notebook
Stack elements...updated
Keyboards
Cameras
Ultrabook
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Element at the top = Keyboards
Count of elements (updated) = 10
Count of elements (updated) = 0
Count of elements(업데이트됨)

예시

이제 다른 예를 살펴보겠습니다 -

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push("Inspiron");
      stack.Push("Alienware");
      stack.Push("Projectors");
      stack.Push("Monitors");
      stack.Push("XPS");
      stack.Push("Laptop");
      stack.Push("Notebook");
      Console.WriteLine("Stack elements...");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements = "+stack.Count);
      Console.WriteLine("Element Speakers is the stack? = "+stack.Contains("Speakers"));
      stack.Push("Headphone");
      stack.Push("Keyboard");
      stack.Push("Earphone");
      Console.WriteLine("\nStack elements...updated");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements (updated) = "+stack.Count);
      Console.WriteLine("Element Alienware is the stack? = "+stack.Contains("Alienware"));
      Stack stack2 = (Stack)stack.Clone();
      Console.WriteLine("\nStack elements...cloned");
      foreach(string val in stack2) {
         Console.WriteLine(val);
      }
      Console.Write("Count of elements (updated) = "+stack2.Count);
      Console.WriteLine("Top of the Stack = "+stack2.Peek());
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Stack elements...
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Count of elements = 7
Element at the top = Notebook
Stack elements...updated
Keyboards
Cameras
Ultrabook
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Element at the top = Keyboards
Count of elements (updated) = 10
Count of elements (updated) = 0
Count of elements(업데이트됨)