- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to return specified number of elements from the end of an array
Use the TakeLast() method to return elements from the end of an array.
Let us first declare and initialize an array.
int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};
Now, let’s get the last three elements.
IEnumerable<int> units = prod.AsQueryable().TakeLast(3);
Let us see the complete code.
Example
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789}; // value of last three products IEnumerable<int> units = prod.AsQueryable().TakeLast(3); foreach (int res in units) { Console.WriteLine(res); } } }
Output
698 765 789
Advertisements