
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Get the object at the beginning of the Queue – Peek Operation in C#
To get the object at the beginning of the Queue, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main() { Queue<string> queue = new Queue<string>(); queue.Enqueue("A"); queue.Enqueue("B"); queue.Enqueue("C"); queue.Enqueue("D"); queue.Enqueue("E"); queue.Enqueue("F"); queue.Enqueue("G"); Console.WriteLine("Count of elements = "+queue.Count); Console.WriteLine("Element at the beginning of queue = " + queue.Peek()); } }
Output
This will produce the following output −
Count of elements = 7 Element at the beginning of queue = A
Example
Let us see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main() { Queue<string> queue = new Queue<string>(); queue.Enqueue("A"); queue.Enqueue("B"); queue.Enqueue("C"); queue.Enqueue("D"); queue.Enqueue("E"); queue.Enqueue("F"); queue.Enqueue("G"); Console.WriteLine("Count of elements = "+queue.Count); Console.WriteLine("Element at the beginning of queue = " + queue.Peek()); queue.Enqueue("H"); queue.Enqueue("I"); queue.Enqueue("J"); Console.WriteLine("Count of elements = "+queue.Count); Console.WriteLine("Element at the beginning of queue = " + queue.Peek()); } }
Output
This will produce the following output −
Count of elements = 7 Element at the beginning of queue = A Count of elements = 10 Element at the beginning of queue = A
- Related Articles
- Add an object to the end of the Queue - Enqueue Operation in C#
- Get object at the top of the Stack in C#
- Difference between peek(), poll() and remove() method of Queue interface in java?
- Get the number of elements contained in the Queue in C#
- Insertion at the beginning in OrderedDict using Python
- Align flex items at the beginning of the container with CSS
- Find the arrangement of queue at given time in C++
- How to insert text at the beginning of the text box in Tkinter?
- Java Program to get the seconds since the beginning of the Java epoch
- Insert the string at the beginning of all items in a list in Python
- Java Program to get the beginning and end date of the week
- Match any string with p at the beginning of it.
- C++ Program to add an element in the array at the beginning
- Golang Program to add an element in the array at the beginning
- PHP program to add item at the beginning of associative array

Advertisements