dotnet / dotnet/csharpstandard

Pointer element access with dynamic index

Open
#729 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

Roslyn appears to allow `P[E]`, where the type of `P` is a pointer type other than `void*`, and the type of `E` is `dynamic`. It generates code to convert the value of `E` to `int`. It however does not allow `P + E` or `*(P + E)` with the same types.

* Does the standard allow or even require this difference between `P[E]` and `*(P + E)`?
* Does the standard specify that `E` is converted from `dynamic` to `int`? Pointer addition operators exist for `int`, `uint`, `long`, and `ulong`.

```csharp
static class C {
static unsafe int Index1(int* p, dynamic i) {
return p[i]; // i is converted to int, why?
}

#if true
static unsafe int Index2(int* p, dynamic i) {
return *(p + i); // error CS0019
}
#endif
}
```

[SharpLab](https://sharplab.io/#v2:CYLg1APgAgjAbAAigJgQYQQbwTgsAKBx1kQFcA7AZwEMAzAUwQEtyAXBASXOHoA8YAFC1YAqBAAcANAmABPctQC2TAMbMAlFgJEdSAOwSA2kwC6AbgQB6S82aUEKgPbkAbvQBOresAStHzNmkAdwALWQB+bSIAXyi8QiIAYiZaX3dSejikeAQKGgYA9i4eXmQhNjEpGXklVQ0tBN19BBEBcQQwDQtrBA93R3d0AGUABhGYAE4s2MaERPpuFIJooA)

Inspired by .

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.