In this tutorial, i’ll show you How to search C# List elements. The BinarySearch method of List<T> searches a sorted list and returns the zero-based index of the found item. The List<T> must be sorted before...
Tag - C# List Examples
How to Reverse a C# List Elements?
The Reverse method of List<T> reverses the order all items in in the List. The following code snippet reverses a List. Example: C# static void Main(string[] args) { List<string> colors = new...
How to Insert Elements at a Position in a C# List?
The Insert method of List class inserts an object at a given position. The first parameter of the method is the 0th based index in the List. The InsertRange method can insert a collection at the given position. Add an item...
List Collection in C#
In c#, List is a generic type of collection, so it will allow storing only strongly typed objects, i.e., elements of the same data type. The size of the list will vary dynamically based on our application requirements, like...
C# initialize list of objects
Instantiate list with values c# C# Code 1: C# class Program { static void Main(string[] args) { var list = new List<string> { "test1", "test2", "test3" }; list.ForEach(n => Console.WriteLine(n)); Console.ReadLine(); } }...
C# Read from Text File and Write to List
In this post we will read a data from a file and store them into a list using ReadAllLines method. C# Code: C# class Program { static void Main(string[] args) { // Specifying a file string path = @"E:\file.txt"; // Calling the...
C# List Find Examples
Example 1: Returns the first occurrence of item matching the specified predicate. C# using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var list = new...
List Equals in C# Examples
Example1: Returns true if the two specified lists are reference equal (are the same instance). C# using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var listA =...
C# List Find and Exists Examples
Example 1: (Exists) Returns true if the list contains items matching the specified predicate. C# using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var list = new...
Copy List Elements to Array using CopyTo Method in C#
Copies all list items into the beginning of the specified array. Using: C# list.CopyTo(arr); 123 list.CopyTo(arr); Example: C# using System; using System.Collections.Generic; using System.Linq; class Program { static...