Difference between Add and AddRange in arrayList c#

Difference between Add and AddRange in arrayList c#

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.

Examples

  1. C# ArrayList Add Method

    • Description: Explains the basic usage of the Add method in ArrayList for adding a single element.
    • Code Implementation:
      // ArrayList Add method for adding a single element ArrayList arrayList = new ArrayList(); arrayList.Add("Item1"); 
  2. C# ArrayList AddRange Method

    • Description: Demonstrates using the AddRange method in ArrayList to add multiple elements from a collection.
    • Code Implementation:
      // ArrayList AddRange method for adding multiple elements ArrayList arrayList = new ArrayList(); ArrayList elementsToAdd = new ArrayList { "Item1", "Item2", "Item3" }; arrayList.AddRange(elementsToAdd); 
  3. C# ArrayList Add vs AddRange with Single Element

    • Description: Compares using Add and AddRange when adding a single element to an ArrayList.
    • Code Implementation:
      // 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 
  4. C# ArrayList Add vs AddRange with Multiple Elements

    • Description: Compares using Add and AddRange when adding multiple elements to an ArrayList.
    • Code Implementation:
      // 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 
  5. C# ArrayList Add vs AddRange Performance

    • Description: Discusses the performance considerations of using Add and AddRange in ArrayList.
    • Code Implementation:
      // 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); 
  6. C# ArrayList Add for Dynamic Elements

    • Description: Illustrates using Add for dynamically adding elements to an ArrayList.
    • Code Implementation:
      // ArrayList Add for dynamically adding elements ArrayList arrayList = new ArrayList(); string dynamicElement = "DynamicItem"; arrayList.Add(dynamicElement); 
  7. C# ArrayList AddRange for Multiple Collections

    • Description: Shows using AddRange to add elements from multiple collections to an ArrayList.
    • Code Implementation:
      // 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); 
  8. C# ArrayList Add for Insertion

    • Description: Demonstrates using Add for inserting an element at a specific index in an ArrayList.
    • Code Implementation:
      // ArrayList Add for insertion at a specific index ArrayList arrayList = new ArrayList(); arrayList.Add("Item1"); arrayList.Insert(1, "Item2"); 
  9. C# ArrayList AddRange for Concatenation

    • Description: Illustrates using AddRange for concatenating two ArrayLists.
    • Code Implementation:
      // ArrayList AddRange for concatenating two ArrayLists ArrayList arrayList1 = new ArrayList { "Item1", "Item2" }; ArrayList arrayList2 = new ArrayList { "Item3", "Item4" }; arrayList1.AddRange(arrayList2); 
  10. C# ArrayList Add vs AddRange with Different Types

    • Description: Discusses the behavior of Add and AddRange when adding elements of different types to an ArrayList.
    • Code Implementation:
      // 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 

More Tags

cakephp-3.x firebase-notifications angular-observable viewport-units pandas-to-sql jackson-databind data-url android-webservice websphere-7 podfile

More C# Questions

More Stoichiometry Calculators

More Electrochemistry Calculators

More General chemistry Calculators

More Physical chemistry Calculators