Add and AddRange are two methods available in the ArrayList class in C# that are used to add elements to the list.
The Add method adds a single element to the end of the ArrayList. It takes a single argument that represents the element to be added.
ArrayList list = new ArrayList(); list.Add("apple"); The AddRange method adds multiple elements to the end of the ArrayList. It takes a single argument that represents a collection of elements to be added.
ArrayList list = new ArrayList(); string[] fruits = { "apple", "banana", "orange" }; list.AddRange(fruits); So, the key difference between Add and AddRange is that Add adds a single element to the end of the ArrayList, while AddRange adds multiple elements to the end of the ArrayList from a collection.
AddRange can be more efficient than multiple Add calls, as it reduces the overhead of resizing the internal array and copying elements into it multiple times. However, AddRange requires a collection as an argument, so it is not as flexible as Add in situations where you need to add elements one at a time or from a non-collection source.
C# ArrayList Add Method
Add method in ArrayList for adding a single element.// ArrayList Add method for adding a single element ArrayList arrayList = new ArrayList(); arrayList.Add("Item1"); C# ArrayList AddRange Method
AddRange method in ArrayList to add multiple elements from a collection.// ArrayList AddRange method for adding multiple elements ArrayList arrayList = new ArrayList(); ArrayList elementsToAdd = new ArrayList { "Item1", "Item2", "Item3" }; arrayList.AddRange(elementsToAdd); C# ArrayList Add vs AddRange with Single Element
Add and AddRange when adding a single element to an ArrayList.// Comparing ArrayList Add vs AddRange with a single element ArrayList arrayList = new ArrayList(); arrayList.Add("Item1"); // Using Add arrayList.AddRange(new ArrayList { "Item2" }); // Using AddRange C# ArrayList Add vs AddRange with Multiple Elements
Add and AddRange when adding multiple elements to an ArrayList.// Comparing ArrayList Add vs AddRange with multiple elements ArrayList arrayList = new ArrayList(); arrayList.Add("Item1"); // Using Add arrayList.AddRange(new ArrayList { "Item2", "Item3" }); // Using AddRange C# ArrayList Add vs AddRange Performance
Add and AddRange in ArrayList.// Performance comparison of ArrayList Add vs AddRange ArrayList arrayList = new ArrayList(); ArrayList elementsToAdd = new ArrayList { "Item1", "Item2", "Item3" }; // Using Add foreach (var element in elementsToAdd) { arrayList.Add(element); } // Using AddRange arrayList.AddRange(elementsToAdd); C# ArrayList Add for Dynamic Elements
Add for dynamically adding elements to an ArrayList.// ArrayList Add for dynamically adding elements ArrayList arrayList = new ArrayList(); string dynamicElement = "DynamicItem"; arrayList.Add(dynamicElement);
C# ArrayList AddRange for Multiple Collections
AddRange to add elements from multiple collections to an ArrayList.// ArrayList AddRange for adding elements from multiple collections ArrayList arrayList = new ArrayList(); ArrayList collection1 = new ArrayList { "Item1", "Item2" }; ArrayList collection2 = new ArrayList { "Item3", "Item4" }; arrayList.AddRange(collection1); arrayList.AddRange(collection2); C# ArrayList Add for Insertion
Add for inserting an element at a specific index in an ArrayList.// ArrayList Add for insertion at a specific index ArrayList arrayList = new ArrayList(); arrayList.Add("Item1"); arrayList.Insert(1, "Item2"); C# ArrayList AddRange for Concatenation
AddRange for concatenating two ArrayLists.// ArrayList AddRange for concatenating two ArrayLists ArrayList arrayList1 = new ArrayList { "Item1", "Item2" }; ArrayList arrayList2 = new ArrayList { "Item3", "Item4" }; arrayList1.AddRange(arrayList2); C# ArrayList Add vs AddRange with Different Types
Add and AddRange when adding elements of different types to an ArrayList.// ArrayList Add vs AddRange with different types ArrayList arrayList = new ArrayList(); arrayList.Add("Item1"); // Adding a string arrayList.AddRange(new ArrayList { 1, 2, 3 }); // Adding integers cakephp-3.x firebase-notifications angular-observable viewport-units pandas-to-sql jackson-databind data-url android-webservice websphere-7 podfile