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 () ?
ConstraintedCopy() does not guarantee success. The first line of the MSDN Docs states:
More specifically the second line :
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().