List related programs in C#
Loading
List related programs in C#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Oct 17, 2023, 3:59 AM EDT
If you're looking for examples of how to use lists in C#, you can use the
Listclass from theSystem.Collections.Genericnamespace. Here are some examples of different list operations:Creating a List:
using System; using System.Collections.Generic; class Program { static void Main() { // Create a list of integers List numbers = new List(); // Add elements to the list numbers.Add(10); numbers.Add(20); numbers.Add(30); // Access elements in the list Console.WriteLine(numbers[0]); // Output: 10 // Iterate through the list foreach (int number in numbers) { Console.WriteLine(number); } } }Adding and Removing Elements:
List colors = new List(); colors.Add("Red"); colors.Add("Green"); colors.Add("Blue"); // Remove an element by value colors.Remove("Green"); // Remove an element by index colors.RemoveAt(0); // Check if an element exists bool containsBlue = colors.Contains("Blue"); // true // Get the number of elements in the list int count = colors.Count; // 1Sorting a List:
List numbers = new List { 5, 3, 1, 4, 2 }; numbers.Sort(); // Sort in ascending order numbers.Reverse(); // Reverse the listSearching in a List:
List names = new List { "Alice", "Bob", "Charlie" }; int index = names.IndexOf("Bob"); // Find the index of an element if (index != -1) { Console.WriteLine("Bob found at index " + index); }Clearing and Copying Lists:
List sourceList = new List { 1, 2, 3 }; List destinationList = new List(); destinationList.AddRange(sourceList); // Copy elements from source to destination sourceList.Clear(); // Clear all elements from the source listThanks
Vishal YelveRank #178 · 13.6K reputationPosted Oct 17, 2023, 4:09 AM EDT
Hi Geethanjali,
do refer below article
https://www.c-sharpcorner.com/article/c-sharp-list/
https://www.c-sharpcorner.com/UploadFile/219d4d/list-in-C-Sharp/
Amit MohantyRank #112 · 20K reputationPosted Oct 17, 2023, 5:27 AM EDT
"List" is a commonly used collection type that represents a dynamic array, allowing you to store and manipulate a collection of elements. They are versatile and often more convenient than arrays when dealing with collections that can change in size over time.
Creating a List: You can create a List in C# by specifying the data type it will hold. For example:
List numbers = new List();This creates an empty list of integers.
Adding Elements: You can add elements to a List using the
Addmethod:numbers.Add(10); numbers.Add(20);Accessing Elements: Elements in a List can be accessed by their index, similar to arrays:
int firstNumber = numbers[0]; // Accesses the first element (10)Iterating Over a List: You can iterate through the elements of a List using a
foreachloop:foreach (int number in numbers) { // Access each element in the list }Finding Elements: You can find the index of an element in the List using the
IndexOfmethod:int index = numbers.IndexOf(20);This will return the index of the first occurrence of the specified element, or -1 if the element is not found.
Removing Elements: To remove an element from the List, you can use the
Removemethod:numbers.Remove(20);This removes the first occurrence of the specified element.
Checking for Element Existence: You can check if a particular element exists in the List using the
Containsmethod:bool contains = numbers.Contains(10);This will return
trueif the element is found in the List.Counting Elements: The
Countproperty gives you the number of elements in the List:int count = numbers.Count;Clearing the List: To remove all elements from the List, you can use the
Clearmethod:This makes the List empty.
Jignesh KumarRank #91 · 22.9K reputationPosted Oct 17, 2023, 9:03 AM EDT
Hi Geethanjali,
Please refer this link which will help you to understand list in C#.
https://www.c-sharpcorner.com/article/c-sharp-list/