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
例
別の例を見てみましょう-
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
-
C#の集計メソッド
C#でAggregateメソッドを使用して、Sum、Min、Max、Averageなどの数学演算を実行します。 Aggregateメソッドを使用して配列要素を乗算する例を見てみましょう。 これが私たちの配列です- int[] arr = { 10, 15, 20 }; ここで、Aggregate()メソッドを使用します- arr.Aggregate((x, y) => x * y); これが完全なコードです- 例 using System; using System.Linq; using System.IO; public class Demo { p
-
C#のTakeWhileメソッド()
TakeWhile()メソッドを使用すると、述語に基づいて条件を設定することでメソッドを取得できます。 まず、配列を宣言して初期化します- int[] arr = { 25, 40, 65, 70}; ここで、TakeWhile()メソッドと述語を使用して、30未満のすべての要素を取得します。 var val = arr.TakeWhile(ele => ele < 30); 同じ例を見てみましょう。ここでは、述語-を使用して30未満の値を表示しています。 例 using System; using System.Linq; using System.IO; public c