C# Program to return specified number of elements from the end of an array

CsharpProgrammingServer Side Programming

<p>Use the TakeLast() method to return elements from the end of an array.</p><p>Let us first declare and initialize an array.</p><pre class="result notranslate">int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};</pre><p>Now, let&rsquo;s get the last three elements.</p><pre class="result notranslate">IEnumerable&lt;int&gt; units = prod.AsQueryable().TakeLast(3);</pre><p>Let us see the complete code.</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/AQqQFC" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">using System; using System.Linq; using System.Collections.Generic; public class Demo { &nbsp; &nbsp;public static void Main() { &nbsp; &nbsp; &nbsp; int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789}; &nbsp; &nbsp; &nbsp; // value of last three products &nbsp; &nbsp; &nbsp; IEnumerable&lt;int&gt; units = prod.AsQueryable().TakeLast(3); &nbsp; &nbsp; &nbsp; foreach (int res in units) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(res); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">698 765 789</pre>
raja
Updated on 23-Jun-2020 07:28:31

Advertisements