In C#, List is a collection type that can be used to store elements of any type in a sequence. Add and Append are both methods of the List class, but they have different behavior:
List.Add() method is used to add a single element to the end of a List. It returns void and takes one argument that represents the element to be added.Example:
var myList = new List<string>(); myList.Add("apple"); myList.Add("banana"); In this example, the Add method is used to add two elements to myList.
List.Append() method is used to append one or more elements to the end of a List. It returns an IEnumerable<T> and takes one or more arguments that represent the elements to be appended.Example:
var myList = new List<string>(); myList = myList.Append("apple").Append("banana").ToList(); In this example, the Append method is used to append two elements to myList. Notice that we are re-assigning the result of Append back to myList. ToList() is used to convert the IEnumerable<T> returned by Append to a List.
In summary, List.Add() is used to add a single element, while List.Append() is used to append one or more elements to a list.
C# List Add Method
Add method in a List to add a single element.// List Add method for adding a single element List<int> myList = new List<int>(); myList.Add(42);
C# List Append Method
Append method in a List and how it differs from Add.// Custom Append method for adding a single element to a List public static class ListExtensions { public static void Append<T>(this List<T> list, T item) { list.Add(item); } } // Usage List<int> myList = new List<int>(); myList.Append(42); C# List Add vs Append with Single Element
Add and a custom Append method when adding a single element to a List.// Comparing List Add vs Append with a single element List<int> myList = new List<int>(); myList.Add(42); // Using Add myList.Append(42); // Using custom Append
C# List AddRange Method
AddRange method in a List to add multiple elements from a collection.// List AddRange method for adding multiple elements List<int> myList = new List<int>(); List<int> elementsToAdd = new List<int> { 1, 2, 3 }; myList.AddRange(elementsToAdd); C# List AppendRange Method
AppendRange method in a List for adding multiple elements.// Custom AppendRange method for adding multiple elements to a List public static class ListExtensions { public static void AppendRange<T>(this List<T> list, IEnumerable<T> items) { list.AddRange(items); } } // Usage List<int> myList = new List<int>(); List<int> elementsToAdd = new List<int> { 1, 2, 3 }; myList.AppendRange(elementsToAdd); C# List AddRange vs AppendRange with Multiple Elements
AddRange and a custom AppendRange method when adding multiple elements to a List.// Comparing List AddRange vs AppendRange with multiple elements List<int> myList = new List<int>(); myList.AddRange(new List<int> { 1, 2, 3 }); // Using AddRange myList.AppendRange(new List<int> { 4, 5, 6 }); // Using custom AppendRange C# List Add for Insertion
Add for inserting an element at a specific index in a List.// List Add for insertion at a specific index List<string> myList = new List<string> { "Item1", "Item2" }; myList.Add("Item3"); // Adding at the end myList.Insert(1, "ItemInserted"); // Inserting at index 1 C# List Append for Dynamic Elements
Append method for dynamically adding elements to a List.// List Append for dynamically adding elements public static class ListExtensions { public static void Append<T>(this List<T> list, T item) { list.Add(item); } } // Usage List<string> myList = new List<string>(); string dynamicElement = "DynamicItem"; myList.Append(dynamicElement); C# List Add for Unique Elements
Add for ensuring unique elements in a List.// List Add for ensuring unique elements List<int> myList = new List<int>(); int elementToAdd = 42; if (!myList.Contains(elementToAdd)) { myList.Add(elementToAdd); } C# List Append for Fluent Syntax
Append method for a fluent syntax when adding elements to a List.// List Append for fluent syntax public static class ListExtensions { public static List<T> Append<T>(this List<T> list, T item) { list.Add(item); return list; } } // Usage List<string> myList = new List<string>(); myList.Append("Item1").Append("Item2").Append("Item3"); nw.js pylint istio arrayofarrays click qml foreign-keys stringbuilder lsusb python-2.7