Posted by: Cirilo Meggiolaro | 03/5/2009

Tip of the day #142 – Array.ConstrainedCopy

The ConstrainedCopy method from the Array class allows you to copy items from a source array to a destination array. It does not resize the destination array though. It just copies items, overwriting items on the destination array. If something goes wrong, the copy is rolled back.

Method overload

public static void ConstrainedCopy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);

How to…

Let’s assume you have the following arrays:

string[] arrA = { “George”, “Max”, “Jack”, “Bill” }; // Source array
string[] arrB = { “Wilson”, “Gregory”, “Monica”, “Janice” }; // Target array

If you want to replace the last two items on arrB with the first two items from the arrA, you’d have to code:

Array.ConstrainedCopy(namesA, 0, namesB, 2, 2);

The array arrB has the following items now: Wilson, Gregory, George, Max.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: