TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# List AddRange, InsertRange (Append Array to List)

Use the List AddRange and InsertRange methods. Append an int array to a List.
AddRange, InsertRange. AddRange adds an entire collection of elements. It can replace tedious foreach-loops that repeatedly call Add on List.ListForeach
We can pass any IEnumerable collection to AddRange, not just an array or another List. InsertRange() operates in a way similar to AddRange, but receives a starting offset position.
AddRange example. We look at the AddRange instance method from the base class library in the C# language on List. This example adds a range at the end of the List using the AddRange method.Int Array

Step 1: A new List is created, and 4 ints are added. An array of 3 elements of the same numeric type is initialized.

Array

Step 2: We call AddRange. The 3 values stored in the array are added to the end of the List.

Step 3: The program displays 7 integers, which are the union of the List itself and the array we added with AddRange.

Console

Tip: The term "range" means an IEnumerable collection. Any type that implements IEnumerable can be used.

IEnumerable
C# program that uses AddRange using System; using System.Collections.Generic; class Program { static void Main() { // Step 1: create List and array. var list = new List<int>(); list.Add(1); list.Add(2); list.Add(5); list.Add(6); var array = new int[3]; array[0] = 7; array[1] = 6; array[2] = 7; // Step 2: call AddRange. list.AddRange(array); // Step 3: display the List contents. foreach (int value in list) { Console.WriteLine("VALUE: {0}", value); } } } Output VALUE: 1 VALUE: 2 VALUE: 5 VALUE: 6 VALUE: 7 VALUE: 6 VALUE: 7
InsertRange example. This method places a collection of elements into a certain index of a List. The List resizes to accommodate the inserted elements.

Step 1: The program populates a new List instance with 4 integral values. Next it creates an array with 3 more values.

Step 2: We invoke InsertRange. The program inserts the array into the List at the second index.

Step 3: We display the contents of the List, which has the contents of the array added at the second index.

C# program that uses InsertRange using System; using System.Collections.Generic; class Program { static void Main() { // Step 1: create List and array. var list = new List<int>(); list.Add(1); list.Add(2); list.Add(5); list.Add(6); var array = new int[3]; array[0] = 7; array[1] = 6; array[2] = 7; // Step 2: call InsertRange. list.InsertRange(1, array); // Step 3: display the elements. foreach (int value in list) { Console.WriteLine("VALUE: {0}", value); } } } Output VALUE: 1 VALUE: 7 VALUE: 6 VALUE: 7 VALUE: 2 VALUE: 5 VALUE: 6
Internals. In the .NET Framework, InsertRange is implemented by using Array.Copy and the CopyTo instance method on arrays. The entire array inside the List will have to be copied.InsertRange Method: Microsoft Docs

Here: This example shows the internal code for InsertRange. If the new range fits in the allocated size, the new range is simply copied.

But: Sometimes 2 Array.Copy calls are needed: one for the elements before, and one for the elements after. A new array is allocated.

InsertRange method body: C# this.EnsureCapacity(this._size + count); if (index < this._size) { Array.Copy(...); } if (this == is2) { Array.Copy(...); Array.Copy(...); } else { T[] array = new T[count]; is2.CopyTo(array, 0); array.CopyTo(...); }
Internals, AddRange. The AddRange method is implemented with InsertRange. AddRange is a wrapper method on top of InsertRange—the target position is 0.
Performance, overview. AddRange and InsertRange are internally the same method. We know from the compiled code above that the internal implementation normally calls Array.Copy or CopyTo.

Therefore: It is impossible for AddRange or InsertRange to perform better than a plain Array.Copy.

However: Array.Copy itself may perform better than manually copying elements in a loop.

Array.Copy
Performance, Add. It may be faster to just use Add() instead of calling AddRange, even if we have an array. Using a foreach-loop and calling Add is faster in the linked benchmark.List Add
LinkedList. If you have a requirement for inserting elements often, LinkedList is faster, as you need to adjust only one reference. But test carefully before committing to LinkedList.LinkedList
A summary. AddRange appends a range of values to the end of a List. InsertRange places an array between the elements in a List. These 2 methods help with List range manipulations.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf