give me an exemple with List
Loading
give me an exemple with List
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
Sudarshan HajareRank #741 · 3.1K reputationPosted Aug 7, 2026, 10:26 PM EDT
Output:
You can also create the list directly:
Then, for example:
List means a list that contains only strings.
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Aug 9, 2026, 1:07 PM EDT
https://www.c-sharpcorner.com/article/c-sharp-list/
Snesh PrajapatiRank #2,336 · 606 reputationPosted Aug 17, 2026, 12:40 AM EDT
Listin C# is a generic collection used to store multiple objects of the same type. T means "Type" and acts as a placeholder. Once you specify the type, the List only accepts that type. In your case you are specifying ListTbecomesstring.If you write List numbers = new List(); then
Tbecomesint.Sangeetha SRank #212 · 11.4K reputationPosted Sep 1, 2026, 6:42 AM EDT
List lines = new List { "Line1", "Line2", "Line3" }; foreach (string line in lines) { Console.WriteLine($"{line}"); }Cynthia SathuragiriRank #937 · 2.2K reputationPosted Sep 2, 2026, 12:59 AM EDT
Here’s a simple example of using
Listin C#. AListcan be used to store multiple text values, such as student names.For example:
List students = new List { "John", "Sarah", "Michael" }; foreach (string student in students) { Console.WriteLine(student); }In this example, the list stores three student names. The
foreachloop goes through each name and prints it. I thinkListis useful because it makes it easy to store and work with a collection of strings.Tuhin PaulRank #7 · 71.9K reputationPosted Sep 20, 2026, 12:19 PM EDT
Listin C# is a collection that can store multiple values of the same type. TheTrepresents the type of data you want to store.For example, if you want to store a list of student names:
using System; using System.Collections.Generic; class Program { static void Main() { List students = new List(); // Add items students.Add("Rahul"); students.Add("Priya"); students.Add("Amit"); // Display items foreach (string student in students) { Console.WriteLine(student); } } }Output:
You can also create the list with values directly:
List numbers = new List { 10, 20, 30, 40 }; foreach (int number in numbers) { Console.WriteLine(number); }Here,
Listmeans the list stores strings, whileListmeans it stores integers.A useful feature of
Listis that its size can grow dynamically when you add more items.ChloeRank #7,584 · 238 reputationPosted Sep 21, 2026, 10:19 PM EDT
using System; using System.Collections.Generic; class Program { static void Main() { List fruits = new List(); fruits.Add("Apple"); fruits.Add("Banana"); fruits.Add("Mango"); foreach (string fruit in fruits) { Console.WriteLine(fruit); } } }Output:
Besides, you can use Spire.XLS to export List to Excel:
using Spire.Xls; class Program { static void Main() { // 1. Prepare the List List fruits = new List { "Apple", "Banana", "Mango" }; // 2. Create the Excel workbook Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // 3. Write the List data into the sheet for (int i = 0; i < fruits.Count; i++) { sheet.Range[i + 1, 1].Value = fruits[i]; } // 4. Save the file workbook.SaveToFile("Fruits.xlsx", ExcelVersion.Version2016); workbook.Dispose(); } }Generated
.xlsx: