Expert C# 5.0: with .NET 4.5 Framework by Mohammad Rahman

Expert C# 5.0: with .NET 4.5 Framework by Mohammad Rahman

Author:Mohammad Rahman
Language: eng
Format: epub
Publisher: Apress®
Published: 2012-12-13T16:00:00+00:00


/* Replace _items with the temporary array which currently holding

* existing contents of the _items array and empty items

* for the expanded cells. */

this._items = destinationArray;

}

else

{

/* Otherwise set _items with 0 items as _emptyArray hold 0 items. */

this._items = List<T>._emptyArray;

}

/*.....*/

}

}

Insert Operation in the List<T>

The InsertRange method works a bit differently in comparison to the Add method. The InsertRange method adds a series of items into the List<T>, starting from a given position of the _items array. From Listing 11-4, you can see that the values {22, 33, 77} are inserted by copying the existing items {3} from the numbers into a new location of the numbers. It started copying items from position 3 of the numbers list and continued until the number of items to copy was equal to the size (4) of the numbers (original sequence) list—the number of items to insert (3). It is also important to calculate the new index where the item will be copied over. The position 6 is calculated using the index plus count, where index refers to the start position of the copy from the source array (provided as input to the InsertRange, which is 3), and count refers to the number of items provided in the InsertRange method to be inserted into the original list. As a result, items {3} will be copied into the array position 6, but the capacity of the original list numbers is four. So it needs to expand the capacity to accommodate more items. After the expansion of the original list, the cell at position {3,4,5} of the expanded original list new values {22,33,77} will be copied. Listing 11-4 shows the usage of the InsertRange method.

Listing 11-4. Example of the InsertRange Method of the List<T> Class

using System;

using System.Collections;

using System.Collections.Generic;



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.