PC Review


Reply
Thread Tools Rate Thread

ConstrainedCopy

 
 
JPS
Guest
Posts: n/a
 
      28th Dec 2006
Can anyone show me an example of how to use the Array.ConstrainedCopy
method. The MSFT example is worthless.

 
Reply With Quote
 
 
 
 
Dave Sexton
Guest
Posts: n/a
 
      28th Dec 2006
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/lib...ainedcopy.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[i]);
}

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Can anyone show me an example of how to use the Array.ConstrainedCopy
> method. The MSFT example is worthless.
>



 
Reply With Quote
 
JPS
Guest
Posts: n/a
 
      28th Dec 2006
Thanks very much

On Dec 28, 11:41 am, "Dave Sexton" <dave@jwa[remove.this]online.com>
wrote:
> 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[i]);
>
> }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
>
> "JPS" <jpsu...@yahoo.com> wrote in messagenews:(E-Mail Removed)...
>
>
>
> > 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 -


 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      28th Dec 2006
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 [C# MVP]" <(E-Mail Removed)> wrote in message
news:6E25A35E-FD0B-441A-96F2-(E-Mail Removed)...
> 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" wrote:
>
>> 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/lib...ainedcopy.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[i]);
>> }
>>
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > Can anyone show me an example of how to use the Array.ConstrainedCopy
>> > method. The MSFT example is worthless.
>> >

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:46 AM.