Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Addition - fun - not what I thought
Message
De
06/07/2009 05:13:09
 
 
À
06/07/2009 04:54:09
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01410144
Message ID:
01410228
Vues:
33
>>>I think the documentation states that in these cases i is only evaluated once for the line. Given that the results will be as found.
>>>e.g i += ++i; // 3 not 4
>>>Best,
>>>Viv
>>
>>
>>Viv,
>>
>>Guess I was looking for side effects
>>
>>If I understand the definition of 'evaluated', then it will be evaluated twice in the example above since it's referenced twice on the right hand side
>>i += ++i becomes i = i + ++i
>
>No. Evaluated once per line not once per reference.
>Given, say i=3 then
>i += ++i;
>would be equivalent to:
>3 += ++3;
>(I know you can't use the operators in this way - but you see what I mean)

My understanding of evaluation - in this case - is fetching the value of i

Your example
  IL_0003:  ldloc.0   // push i on the stack (first time) [ stack = 3]
  IL_0004:  ldloc.0  // push i on the stack (second time) [ stack = 3 3]
  IL_0005:  ldc.i4.1 // push constant 1 on the stack [ stack = 3 3 1]
  IL_0006:  add    // add [stack = 3 4]
  IL_0007:  dup  // [ stack = 3 4 4]
  IL_0008:  stloc.0 // pop stack and put the value in i [stack = 3 4]
  IL_0009:  add // add [stack = 7]
  IL_000a:  stloc.0  // pop stack and put the value in i [stack = ]
in the case below, it has to fetch the value of i 4 times

int i = 3;
i += ++i + ++i + ++i; // i = 18


Update:
  IL_0003:  ldloc.0  // 1
  IL_0004:  ldloc.0 // 2
  IL_0005:  ldc.i4.1
  IL_0006:  add
  IL_0007:  dup
  IL_0008:  stloc.0
  IL_0009:  ldloc.0 // 3
  IL_000a:  ldc.i4.1
  IL_000b:  add
  IL_000c:  dup
  IL_000d:  stloc.0
  IL_000e:  add
  IL_000f:  ldloc.0  // 4
  IL_0010:  ldc.i4.1
  IL_0011:  add
  IL_0012:  dup
  IL_0013:  stloc.0
  IL_0014:  add
  IL_0015:  add
  IL_0016:  stloc.0
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform