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
Copy file name to clipboardExpand all lines: standard/unsafe-code.md
+7-1Lines changed: 7 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -787,22 +787,28 @@ In an unsafe context, the `+` operator ([§12.13.5](expressions.md#12135-additio
787
787
```csharp
788
788
T*operator+(T*x, inty);
789
789
T*operator+(T*x, uinty);
790
+
T*operator+(T*x, ninty);
791
+
T*operator+(T*x, nuinty);
790
792
T*operator+(T*x, longy);
791
793
T*operator+(T*x, ulongy);
792
794
T*operator+(intx, T*y);
793
795
T*operator+(uintx, T*y);
796
+
T*operator+(nintx, T*y);
797
+
T*operator+(nuintx, T*y);
794
798
T*operator+(longx, T*y);
795
799
T*operator+(ulongx, T*y);
796
800
T*operator –(T*x, inty);
797
801
T*operator –(T*x, uinty);
802
+
T*operator-(T*x, ninty);
803
+
T*operator-(T*x, nuinty);
798
804
T*operator –(T*x, longy);
799
805
T*operator –(T*x, ulongy);
800
806
longoperator –(T*x, T*y);
801
807
```
802
808
803
809
There are no predefined operators for pointer addition or subtraction with native integer ([§8.3.6](types.md#836-integral-types)) offsets. Instead, `nint` and `nuint` values shall be promoted to `long` and `ulong`, respectively, with pointer arithmetic using the predefined operators for those types.
804
810
805
-
Given an expression `P` of a data pointer type `T*` and an expression `N` of type `int`, `uint`, `long`, or `ulong`, the expressions `P + N` and `N + P` compute the pointer value of type `T*` that results from adding `N * sizeof(T)` to the address given by `P`. Likewise, the expression `P – N` computes the pointer value of type `T*` that results from subtracting `N * sizeof(T)` from the address given by `P`.
811
+
Given an expression `P` of a data pointer type `T*` and an expression `N` of type `int`, `uint`, `nint`, `nuint`, `long`, or `ulong`, the expressions `P + N` and `N + P` compute the pointer value of type `T*` that results from adding `N * sizeof(T)` to the address given by `P`. Likewise, the expression `P – N` computes the pointer value of type `T*` that results from subtracting `N * sizeof(T)` from the address given by `P`.
806
812
807
813
Given two expressions, `P` and `Q`, of a data pointer type `T*`, the expression `P – Q` computes the difference between the addresses given by `P` and `Q` and then divides that difference by `sizeof(T)`. The type of the result is always `long`. In effect, `P - Q` is computed as `((long)(P) - (long)(Q)) / sizeof(T)`.
0 commit comments