You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We stumbled across this one while implementing the deconstruction feature in ILSpy's decompiler engine.
Version Used:
Microsoft (R) Visual C# Compiler version 3.7.0-6.20375.2 (34202cc)
Steps to Reproduce:
compile the following code using csc.exe:
using System;publicclassC{public(int,int)GetTuple(){
Console.WriteLine("GetTuple");return(0,1);}publicstaticvoidMain()=>new C().M();publicvoidM(){try{
Console.WriteLine("before deconstruction");(GetArray<int>()[GetIndex()], _)= GetTuple();
Console.WriteLine("after deconstruction");}catch(Exceptionex){
Console.WriteLine(ex.GetType().FullName);}try{
Console.WriteLine("before normal access");GetArray<int>()[GetIndex()]= GetTuple().Item1;
Console.WriteLine("after normal access");}catch(Exceptionex){
Console.WriteLine(ex.GetType().FullName);}}static T GetValue<T>(){
Console.WriteLine("GetValue");returndefault(T);}static T[]GetArray<T>(){
Console.WriteLine("GetArray");returnnull;}staticintGetIndex(){
Console.WriteLine("GetIndex");return0;}}
run the compiled exe
Expected Behavior:
I would expect the evaluation order involving deconstruction to match the evaluation order of normal array access, that means, evaluate the expression from left to right:
GetArray<int>()[GetIndex()]= GetTuple().Item1;
executes as follows:
GetArray
GetIndex
GetTuple
so it produces the following output:
before deconstruction
GetArray
GetIndex
GetTuple
System.NullReferenceException
before normal access
GetArray
GetIndex
GetTuple
System.NullReferenceException
Actual Behavior:
It produces the following output:
before deconstruction
GetArray
GetIndex
System.NullReferenceException
before normal access
GetArray
GetIndex
GetTuple
System.NullReferenceException
Notice that the evaluation of the GetTuple call never occurs in the deconstruction case, unlike the normal array access case.
Is this a bug or just an inconsistency in the deconstruction evaluation order compared to "standard" C# rules?
The text was updated successfully, but these errors were encountered:
Yes, the order of evaluation is specified and this order is expected.
In a deconstruction, we completely evaluate the left-hand side expressions first, as we do in a compound assignment. But in a simple assignment, the indexing is delayed a bit. I find the behavior of simple assignment a bit more surprising myself :-/
I'll go ahead and close the issue as by-design. Thanks
We stumbled across this one while implementing the deconstruction feature in ILSpy's decompiler engine.
Version Used:
Microsoft (R) Visual C# Compiler version 3.7.0-6.20375.2 (34202cc)
Steps to Reproduce:
Expected Behavior:
I would expect the evaluation order involving deconstruction to match the evaluation order of normal array access, that means, evaluate the expression from left to right:
executes as follows:
so it produces the following output:
Actual Behavior:
It produces the following output:
Notice that the evaluation of the
GetTuple
call never occurs in the deconstruction case, unlike the normal array access case.Is this a bug or just an inconsistency in the deconstruction evaluation order compared to "standard" C# rules?
The text was updated successfully, but these errors were encountered: