This method returns the object at the beginning of the Queue without removing it. This method is similar to the Dequeue method, but Peek does not modify the Queue and is an O(1) operation. This method comes under System.Collections namespace.
public virtual object Peek ();
Return Value: The object at the beginning of the Queue.
Exception: This method will give InvalidOperationException if the Queue is empty.
Below programs illustrate the use of above-discussed method:
Total number of elements in the Queue are : 6 Element at the beginning is : 1st Element Element at the beginning is : 1st Element Total number of elements in the Queue are : 6
System.InvalidOperationException: Queue empty.
- Difference between Method Overriding and Method Hiding in C#
- C# | Math.Tan() Method
- C# | Math.Cos() Method
- C# | Math.Sin() Method
- C# | Method Overriding
- C# | IsNullOrEmpty() Method
- C# | Math.Min() Method
- C# | Dictionary.Add() Method
- C# | String.Contains() Method
- Stack.Contains() Method in C#
- Queue.Contains() Method in C#
- C# | Math.Log() Method
- DateTime.Add() Method in C#
- C# | SortedDictionary.Add() Method
- Method Hiding in C#
- Stack.Pop() Method in C#
- C# | StartsWith() Method
- C# | CopyTo() Method
- C# | Uri.IsWellFormedOriginalString() Method
- C# | Uri.ToString() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.