Skip to content

Commit 8a237f5

Browse files
author
Ron Petrusha
authored
Documented Unsafe.Unbox method (#2316)
* Began documented Unbox * Documented usage of Unsafe.Unbox * Addressed feedback
1 parent 37dd7d6 commit 8a237f5

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

xml/System.Runtime.CompilerServices/Unsafe.xml

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,11 +1099,39 @@
10991099
<Parameter Name="box" Type="System.Object" Index="0" FrameworkAlternate="dotnet-plat-ext-3.0" />
11001100
</Parameters>
11011101
<Docs>
1102-
<typeparam name="T">To be added.</typeparam>
1103-
<param name="box">To be added.</param>
1104-
<summary>To be added.</summary>
1105-
<returns>To be added.</returns>
1106-
<remarks>To be added.</remarks>
1102+
<typeparam name="T">The type to be unboxed.</typeparam>
1103+
<param name="box">The value to unbox.</param>
1104+
<summary>Returns a <see langword="mutable ref" /> to a boxed value.</summary>
1105+
<returns>A <see langword="mutable ref" /> to the boxed value <paramref name="box" />.</returns>
1106+
<remarks>
1107+
<format type="text/markdown"><![CDATA[
1108+
1109+
The `Unbox<T>` method is simply a wrapper for the IL [unbox](xref:System.Reflection.Emit.OpCodes.Unbox) instruction. It is useful as a performance optimization. Whenever an API that takes an <xref:System.Object> needs to be called repeatedly with different values of a value type, the same box object can be reused rather than a new one created each time.
1110+
1111+
The `Unbox<T>` method has an important usage constraint that is not enforced by language compilers and that is the responsibility of the caller. The IL `unbox` instruction returns a controlled-mutability managed pointer. Because .NET and .NET language compilers cannot represent this constraint, the `Unbox<T>` method returns a normal mutable `ref T`. However developers **must not** mutate the returned reference unless they are certain that `T` is a mutable struct type. For example, because the numeric primitives such as <xref:System.Int32> are not mutable struct types, the following code is not suppported:
1112+
1113+
```csharp
1114+
Unsafe.Box<int>(obj) = 30;
1115+
```
1116+
1117+
In contrast, a type such as <xref:System.Drawing.Point?<displayProperty=nameWithType> is a mutable struct with public property setters, so the following code is supported:
1118+
1119+
```csharp
1120+
Unsafe.Unbox<System.Drawing.Point>(obj).X = 50;
1121+
```
1122+
1123+
For more information, see sections III.1.8.1.2.2 ("Controlled-muttability managed pointers") and III.4.32 ("unbox -- convert boxed value type to its raw form") in [ECMA-335: Common Language Infrastructure (CLI) Partitions I to VI](https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf).
1124+
1125+
]]></format>
1126+
</remarks>
1127+
<exception cref="T:System.NullReferenceException"><paramref name="box" /> is <see langword="null" />, and <typeparamref name="T" /> is a non-nullable value type.</exception>
1128+
<exception cref="T:System.InvalidCastException"><paramref name="box" /> is not a boxed value type.
1129+
1130+
-or-
1131+
1132+
<paramref name="box" /> is not a boxed <typeparamref name="T" />.
1133+
</exception>
1134+
<exception cref="T:System.TypeLoadException"><typeparamref name="T" /> cannot be found.</exception>
11071135
</Docs>
11081136
</Member>
11091137
<Member MemberName="Write&lt;T&gt;">

0 commit comments

Comments
 (0)