8

I was going through the source code for Array.cs when I read that Array.Copy() does not provide a guarantee that a copy would be successful and infact even possibly corrupting the original instance (Correct me if I am wrong here). To provide peace of mind, ConstrainedCopy () seems to achieve the same.

My question is:
1> Why would anyone use Array.Copy() if it doesn't seem to guarantee a successful transfer of data and going on to possibly hurt the original instance? Infact, all collection classes seem to use Array.Copy() to increase their instance size. Why not use ConstrainedCopy() here

2> How much would be the cost of using ConstrainedCopy () all the time then? I am assuming there would be more logic added to ConstrainedCopy () ?

3 Answers 3

20
Object[] objArray = { "Anakin", "Skywalker", 666 };
String[] stringArray = new String[3];
  1. Array.Copy(objArray, stringArray , 3);

    This throws an invalid cast exception. Even after the exception is thrown (if you swallow it), the first two elements of the objArray are copied to the stringArray.

  2. Array.ConstrainedCopy(objArray, 0, stringArray, 0, 3);

    This throws an System.ArrayTypeMismatchException and won't copy any elements to the destination array (stringArray).

Sign up to request clarification or add additional context in comments.

Comments

5

ConstraintedCopy() does not guarantee success. The first line of the MSDN Docs states:

Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely.

More specifically the second line :

Guarantees that all changes are undone if the copy does not succeed completely.

An exception can still be thrown in very extreme circumstances.However those circumstances are exceptional and you shouldn't have to worry about them in most scenarios.

In short, just stick with Array.Copy().

3 Comments

Can you explain what those extreme circumstances are that can cause exceptions? Otherwise we won't know whether our scenario is one where we need to worry.
Touche, something like an out of memory expection can cause a failure of the copy, most of these circumstances are outside the scope of the copy method and usually are a sign of a bigger problem.
Most exceptions that can be thrown from the Copy method all happen before the copying starts, so you don't need ConstrainedCopy to save you from an index out of range or ype mismatch. The only possible exceptions I can think of that could happen mid-copy are InvalidCastException and ThreadAbortException.
2

See this question: C# quickest way to shift array

ConstrainedCopy is slightly slower, but not significantly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.