dotnet / dotnet/csharpstandard

12.7.5 and 12.18.2 Member access evaluation order for assignments

Open
#401 0 comments 0 reactions 1 assignee Claimed by @MadsTorgersen View on GitHub
status: gnarly type: bug
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

This is a companion issue to https://github.com/dotnet/csharpstandard/issues/169

12.7.5 (member access) suggests that evaluating a member access expression performs a nullity check.

12.18.2 (simple assignment) states for an assignment `x = y`:

> - If x is classified as a variable:
> - x is evaluated to produce the variable.
> - y is evaluated and, if required, converted to the type of x through an implicit conversion (§12.2).

That all sounds fine, but my understanding of reality is that the nullity check is only performed *after* `y` is evaluated. [Sample code to show that](https://sharplab.io/#v2:EYLgtghglgdgNAFxAJwK7wCYgNQB8ACAzAAT4BMxAisQN4CwAUMc6SbAsQB4DcjjLpMhXpMBLagEdiAXmIxUAGwW9RY4hIB0nGcQDKUDAFMAogDNThgMYIAFAEoVAgL78Wr5kVIBGAGzF2egYm5la2du60EQL4XhoxAJw2AET6RmYW1kkOUSz4AOzEZI4sLgylQA)...

```csharp
public class Q {
public int x;

public static void Main() {
Q q = null;
q.x = SideEffect();
}

public static int SideEffect()
{
System.Console.WriteLine("SideEffect");
return 2;
}
}
```

Assigning to Mads initially as he has been assigned https://github.com/dotnet/csharpstandard/issues/169

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.