Queue with Examples in C#?

CsharpServer Side ProgrammingProgramming

<p>The Queue class in C# is the first-in, first-out collection of objects. Let us see some of the methods of the Queue class in C# &minus;</p><table class="table table-bordered"><thead><tr><th>Sr.No</th><th style="text-align: center;">Methods &amp; Description</th></tr></thead><tbody><tr><td style="text-align: center;">1</td><td><strong>Clear()</strong><br>Removes all objects from the Queue&lt;T&gt;.<br></td></tr><tr><td style="text-align: center;">2</td><td><strong>Contains(T)</strong><br>Determines whether an element is in the Queue&lt;T&gt;.<br></td></tr><tr><td style="text-align: center;">3</td><td><strong>CopyTo(T[], Int32)</strong><br>Copies the Queue&gt;T&lt; elements to an existing onedimensional Array, starting at the specified array index.<br></td></tr><tr><td style="text-align: center;">4</td><td><strong>Dequeue()</strong><br>Removes and returns the object at the beginning of the Queue&lt;T&gt;.<br></td></tr><tr><td style="text-align: center;">5</td><td><strong>Enqueue(T)</strong><br>Adds an object to the end of the Queue&lt;T&gt;.<br></td></tr><tr><td style="text-align: center;">6</td><td><strong>Equals(Object)</strong><br>Determines whether the specified object is equal to the current object.(Inherited from Object)<br></td></tr><tr><td style="text-align: center;">7</td><td><strong>GetEnumerator()</strong><br>Returns an enumerator that iterates through the Queue&lt;T&gt;<br></td></tr><tr><td style="text-align: center;">8</td><td><strong>GetHashCode()</strong><br>Serves as the default hash function. (Inherited from Object)<br></td></tr><tr><td style="text-align: center;">9</td><td><strong>GetType()</strong><br>Gets the Type of the current instance.<br></td></tr></tbody></table><h2>Example</h2><p>Let us now see some examples &minus;</p><p>To get the object at the beginning of the Queue, the code is as follows &minus;</p><p><a class="demo" href="http://tpcg.io/KbX04snk" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">using System; using System.Collections.Generic; public class Demo { &nbsp; &nbsp;public static void Main() { &nbsp; &nbsp; &nbsp; Queue&lt;string&gt; queue = new Queue&lt;string&gt;(); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;A&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;B&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;C&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;D&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;E&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;F&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;G&quot;); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Count of elements = &quot;+queue.Count); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Element at the beginning of queue = &quot; + queue.Peek()); &nbsp; &nbsp;} }</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">Count&nbsp;of&nbsp;elements&nbsp;=&nbsp;7 Element&nbsp;at&nbsp;the&nbsp;beginning&nbsp;of&nbsp;queue&nbsp;=&nbsp;A</pre><p>To remove all objects from the Queue, the code is as follows &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/TRAGXgQ7" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">using System; using System.Collections.Generic; public class Demo { &nbsp; &nbsp;public static void Main() { &nbsp; &nbsp; &nbsp; Queue&lt;string&gt; queue = new Queue&lt;string&gt;(); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;Gary&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;Jack&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;Ryan&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;Kevin&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;Mark&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;Jack&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;Ryan&quot;); &nbsp; &nbsp; &nbsp; queue.Enqueue(&quot;Kevin&quot;); &nbsp; &nbsp; &nbsp; Console.Write(&quot;Count of elements = &quot;); &nbsp; &nbsp; &nbsp; Console.WriteLine(queue.Count); &nbsp; &nbsp; &nbsp; queue.Clear(); &nbsp; &nbsp; &nbsp; Console.Write(&quot;Count of elements (updated) = &quot;); &nbsp; &nbsp; &nbsp; Console.WriteLine(queue.Count); &nbsp; &nbsp;} }</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">Count&nbsp;of&nbsp;elements&nbsp;=&nbsp;8 Count&nbsp;of&nbsp;elements&nbsp;(updated)&nbsp;=&nbsp;0</pre>
raja
Updated on 16-Dec-2019 08:18:16

Advertisements