
- 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
C# Program to get the first three elements from a list
Use the Take() method to get the first individual number of elements in C#.
Firstly, set a list and add elements −
List<string> myList = new List<string>(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four"); myList.Add("Five"); myList.Add("Six");
Now, use the Take() method to get the elements from the list. Add the number of the elements you want as an argument −
myList.Take(3)
Here is the code −
Example
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { List<string> myList = new List<string>(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four"); myList.Add("Five"); myList.Add("Six"); // first three elements var res = myList.Take(3); // displaying the first three elements foreach (string str in res) { Console.WriteLine(str); } } }
Output
One Two Three
- Related Articles
- Java Program to Get First and Last Elements from an Array List
- C# Program to display the last three elements from a list in reverse order
- Python program to get first and last elements from a tuple
- C# program to remove duplicate elements from a List
- Get first three letters from every string in C#
- C++ Program to get the first item from the array
- C# program to get the List of keys from a Dictionary
- C# Program to get the smallest and largest element from a list
- Get first and last elements of a list in Python
- C# program to create a List with elements from an array
- Python program to interchange first and last elements in a list
- Program to find three unique elements from list whose sum is closest to k Python
- Golang Program to delete the first node from a linked list.
- C++ Program to get the first given number of items from the array
- How to pop the first element from a C# List?

Advertisements