Just playing around with some of the APIs in .NET and I can't seem to find a way to cause Array.ConstrainedCopy() fail.
According to MSDN, it's treated as an atomic operation. If it fails during the copy, the entire call fails resulting in no elements being copied as opposed to its Array.Copy() counterpart.
Can someone demonstrate this or tell me how to do this?
This code fails in both types of copy. I'd like to see an example of Array.Copy() only copying some elements to get a better understanding of where I might use either forms of copy.
object[] yer = new object[] { "as", "qwe", "re", 1 };
string[] copy = new string[yer.Length];
Array.ConstrainedCopy(yer, 0, copy, 0, yer.Length); // runtime error
Array.Copy(yer, 0, copy, 0, yer.Length); //runtime error
You could be copying an array of type Object[] to an array of type String[]. When one of the elements in the source array cannot be cast to a string, the copy will fail. Array.Copy will just stop where the error occurred, while Array.ConstrainedCopy will roll back the complete operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With