ConstrainedCopy

  • Thread starter Thread starter JPS
  • Start date Start date
J

JPS

Can anyone show me an example of how to use the Array.ConstrainedCopy
method. The MSFT example is worthless.
 
Hi,

What MSFT example do you mean?

It's basically the same thing as Copy except it ensures that the operation
is atomic, so that in the case of an exception it will leave the destination
array in the state that it was in before the operation was attempted.
Other, more subtle differences between the Copy and the ConstrainedCopy
methods are explained in the following documentation:

Array.ConstrainedCopy Method
http://msdn2.microsoft.com/en-us/library/system.array.constrainedcopy.aspx

Here's a simple example:

int[] array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] array2 = new int[20];

Array.ConstrainedCopy(array1, 0, array2, 5, 10);

for (int i = 0; i < array2.Length; i++)
{
Console.WriteLine("{0}: {1}", i, array2);
}

Output:

0: 0
1: 0
2: 0
3: 0
4: 0
5: 1
6: 2
7: 3
8: 4
9: 5
10: 6
11: 7
12: 8
13: 9
14: 10
15: 0
16: 0
17: 0
18: 0
19: 0
 
Thanks very much

Hi,

What MSFT example do you mean?

It's basically the same thing as Copy except it ensures that the operation
is atomic, so that in the case of an exception it will leave the destination
array in the state that it was in before the operation was attempted.
Other, more subtle differences between the Copy and the ConstrainedCopy
methods are explained in the following documentation:

Array.ConstrainedCopy Methodhttp://msdn2.microsoft.com/en-us/library/system.array.constrainedcopy...

Here's a simple example:

int[] array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] array2 = new int[20];

Array.ConstrainedCopy(array1, 0, array2, 5, 10);

for (int i = 0; i < array2.Length; i++)
{
Console.WriteLine("{0}: {1}", i, array2);

}Output:

0: 0
1: 0
2: 0
3: 0
4: 0
5: 1
6: 2
7: 3
8: 4
9: 5
10: 6
11: 7
12: 8
13: 9
14: 10
15: 0
16: 0
17: 0
18: 0
19: 0

--
Dave Sextonhttp://davesexton.com/blog



Can anyone show me an example of how to use the Array.ConstrainedCopy
method. The MSFT example is worthless.- Hide quoted text -- Show quoted text -
 
Hi Peter,

Yes, I did not mean atomic as in thread-safe, I meant atomic as in a
transaction.

--
Dave Sexton
http://davesexton.com/blog

Peter Ritchie said:
ConstrainedCopy is not documented to be atomic and it is not documented to
be
thread safe. It's meant to be used when you want to ensure the
destination
array will not be corrupt in the presence of an exception.
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


Dave Sexton said:
Hi,

What MSFT example do you mean?

It's basically the same thing as Copy except it ensures that the
operation
is atomic, so that in the case of an exception it will leave the
destination
array in the state that it was in before the operation was attempted.
Other, more subtle differences between the Copy and the ConstrainedCopy
methods are explained in the following documentation:

Array.ConstrainedCopy Method
http://msdn2.microsoft.com/en-us/library/system.array.constrainedcopy.aspx

Here's a simple example:

int[] array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] array2 = new int[20];

Array.ConstrainedCopy(array1, 0, array2, 5, 10);

for (int i = 0; i < array2.Length; i++)
{
Console.WriteLine("{0}: {1}", i, array2);
}

Output:

0: 0
1: 0
2: 0
3: 0
4: 0
5: 1
6: 2
7: 3
8: 4
9: 5
10: 6
11: 7
12: 8
13: 9
14: 10
15: 0
16: 0
17: 0
18: 0
19: 0

--
Dave Sexton
http://davesexton.com/blog

JPS said:
Can anyone show me an example of how to use the Array.ConstrainedCopy
method. The MSFT example is worthless.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top