Click here to Skip to main content
15,913,027 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: Theory and practice of XML layout? Pin
PIEBALDconsult15-Sep-10 15:54
mvePIEBALDconsult15-Sep-10 15:54 
GeneralYou can't assign to an array element within a For Each loop! [modified] Pin
Peter R. Fletcher13-Sep-10 5:17
Peter R. Fletcher13-Sep-10 5:17 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 6:13
sitebuilderLuc Pattyn13-Sep-10 6:13 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Peter R. Fletcher13-Sep-10 8:23
Peter R. Fletcher13-Sep-10 8:23 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 8:33
sitebuilderLuc Pattyn13-Sep-10 8:33 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Peter R. Fletcher13-Sep-10 9:24
Peter R. Fletcher13-Sep-10 9:24 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 9:28
sitebuilderLuc Pattyn13-Sep-10 9:28 
GeneralRe: You can't assign to an array element within a For Each loop! [modified] Pin
harold aptroot13-Sep-10 11:18
harold aptroot13-Sep-10 11:18 
Actually, they do make a copy, even in the optimized code.
In fact, two copies are made, at least in the code I tested.

See here:
ASM
shl         rcx,4 
mov         eax,dword ptr [rbx+rcx+10h] 
mov         dword ptr [rsp+20h],eax 
mov         eax,dword ptr [rbx+rcx+14h] 
mov         dword ptr [rsp+24h],eax 
mov         eax,dword ptr [rbx+rcx+18h] 
mov         dword ptr [rsp+28h],eax 
mov         eax,dword ptr [rbx+rcx+1Ch] 
mov         dword ptr [rsp+2Ch],eax 
lea         rcx,[rsp+20h] 
mov         rax,qword ptr [rcx] 
mov         qword ptr [rsp+40h],rax 
mov         rax,qword ptr [rcx+8] 
mov         qword ptr [rsp+48h],rax 
lea         rax,[rsp+40h] 
movss       xmm5,dword ptr [rax] 
; the second copy is read back

The C# code:
static void Normalize(Float3[] array)
{
    Float3 x = new Float3();
    foreach (Float3 f in array)
    {
        x = f.Normalize();
    }
    if (array.Length != 0)
        throw new Exception(x.ToString());  //to make it easier to attach the debugger
}

edit: made a slight mistake here, whatever.
If you change "the foreach value" (indirectly, of course), the second copy is affected but not the first. The first copy is never used again.
For primitive types, the code is actually sane.

modified on Monday, September 13, 2010 5:39 PM

GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 11:25
sitebuilderLuc Pattyn13-Sep-10 11:25 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
harold aptroot13-Sep-10 11:29
harold aptroot13-Sep-10 11:29 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 11:43
sitebuilderLuc Pattyn13-Sep-10 11:43 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
harold aptroot13-Sep-10 12:00
harold aptroot13-Sep-10 12:00 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
DaveyM6913-Sep-10 12:27
professionalDaveyM6913-Sep-10 12:27 
Questionhow do I auto relocate component when resizing the window? Pin
bimbambumbum12-Sep-10 14:09
bimbambumbum12-Sep-10 14:09 
AnswerRe: how do I auto relocate component when resizing the window? Pin
PIEBALDconsult12-Sep-10 14:30
mvePIEBALDconsult12-Sep-10 14:30 
GeneralRe: how do I auto relocate component when resizing the window? Pin
bimbambumbum12-Sep-10 14:39
bimbambumbum12-Sep-10 14:39 
Questiondatagridview Pin
Jefry boycot12-Sep-10 2:28
Jefry boycot12-Sep-10 2:28 
AnswerRe: datagridview Pin
Abhinav S12-Sep-10 3:45
Abhinav S12-Sep-10 3:45 
GeneralRe: datagridview Pin
Jefry boycot16-Sep-10 7:17
Jefry boycot16-Sep-10 7:17 
QuestionVS2010 Pin
Herboren11-Sep-10 8:01
Herboren11-Sep-10 8:01 
AnswerRe: VS2010 Pin
Abhinav S11-Sep-10 19:43
Abhinav S11-Sep-10 19:43 
AnswerRe: VS2010 Pin
Thomas Stockwell12-Sep-10 8:32
professionalThomas Stockwell12-Sep-10 8:32 
AnswerRe: VS2010 Pin
PIEBALDconsult12-Sep-10 16:37
mvePIEBALDconsult12-Sep-10 16:37 
QuestionPutting window inside the desktop Pin
Khao9-Sep-10 17:41
Khao9-Sep-10 17:41 
AnswerRe: Putting window inside the desktop Pin
Thomas Stockwell12-Sep-10 8:34
professionalThomas Stockwell12-Sep-10 8:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.