|
1099 | 1099 | <Parameter Name="box" Type="System.Object" Index="0" FrameworkAlternate="dotnet-plat-ext-3.0" />
|
1100 | 1100 | </Parameters>
|
1101 | 1101 | <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">< 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> |
1107 | 1135 | </Docs>
|
1108 | 1136 | </Member>
|
1109 | 1137 | <Member MemberName="Write<T>">
|
|
0 commit comments