C# List Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
Here are some examples of working with lists in C#:
List<int> list1 = new List<int> { 1, 2, 3 }; List<int> list2 = new List<int> { 4, 5, 6 }; List<int> combinedList = list1.Concat(list2).ToList();
In this example, two List<int>
objects are created, and then they are concatenated using the Concat
method. The resulting combined list is then converted to a new List<int>
using the ToList
method.
List<string> names = new List<string> { "Alice", "Bob", "Charlie", "Dave" }; string commaSeparatedNames = string.Join(",", names);
In this example, a List<string>
is initialized with several string values, and then the list is converted to a comma-separated string using the string.Join
method.
class Person { public string Name { get; set; } public int Age { get; set; } } List<Person> people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 20 }, new Person { Name = "Charlie", Age = 25 } }; string commaSeparatedNames = string.Join(",", people.Select(p => p.Name));
In this example, a list of Person
objects is created, and then the Name
property of each object is concatenated into a comma-separated string using the Select
method and the string.Join
method.
List<List<string>> listOfLists = new List<List<string>> { new List<string> { "Alice", "Bob", "Charlie" }, new List<string> { "Dave", "Eve" }, new List<string> { "Frank" } }; string delimiter = "|"; string joinedString = string.Join(delimiter, listOfLists.Select(l => string.Join(",", l)));
In this example, a List<List<string>>
is initialized with several lists of string values. The Select
method is used to project each inner list to a comma-separated string, and then the outer list is joined into a single string using the string.Join
method with a specified delimiter.
C# concatenate strings from List:
List<string> stringList = new List<string> { "Hello", " ", "World" }; string concatenatedString = string.Concat(stringList);
Joining strings in a List in C#:
List<string> words = new List<string> { "This", "is", "a", "sentence" }; string sentence = string.Join(" ", words);
Concatenation of elements in List<string> C#:
List<string> items = new List<string> { "Item1", "Item2", "Item3" }; string result = items.Aggregate((current, next) => current + next);
C# LINQ concatenate strings in List:
List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = string.Concat(words.Select(word => word + " "));
Concatenating List of objects to string in C#:
List<Person> persons = new List<Person> { /* List of Person objects */ }; string concatenatedNames = string.Join(", ", persons.Select(person => person.Name));
C# StringBuilder concatenate strings from List:
List<string> words = new List<string> { "This", "is", "a", "sentence" }; StringBuilder sb = new StringBuilder(); words.ForEach(word => sb.Append(word)); string result = sb.ToString();
Joining elements in List<string> with delimiter in C#:
List<string> items = new List<string> { "Item1", "Item2", "Item3" }; string result = string.Join(", ", items);
C# concatenate strings with newline from List:
List<string> lines = new List<string> { "Line 1", "Line 2", "Line 3" }; string concatenatedLines = string.Join(Environment.NewLine, lines);
C# concatenate strings in List with separator:
List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = string.Join(" - ", words);
Concatenating strings from List with foreach loop in C#:
List<string> words = new List<string> { "This", "is", "a", "sentence" }; string result = ""; foreach (var word in words) { result += word; }
C# concatenate strings in List using + operator:
List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = string.Empty; foreach (var word in words) { concatenatedString += word; }
Join List<string> to comma-separated string in C#:
List<string> items = new List<string> { "Item1", "Item2", "Item3" }; string result = string.Join(",", items);
C# concatenate strings from List with specific condition:
List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = string.Concat(words.Where(word => word.Length > 1));
Concatenating List<string> using String.Concat in C#:
List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = String.Concat(words);
C# LINQ join List<string> to a single string:
List<string> words = new List<string> { "This", "is", "a", "sentence" }; string result = string.Join(" ", words);
Concatenate unique strings from List<string> in C#:
List<string> items = new List<string> { "Item1", "Item2", "Item1", "Item3" }; string result = string.Join(", ", items.Distinct());