Skip to content

Commit a264579

Browse files
Youssef1313BillWagner
authored andcommitted
Clarify CLR acronym (#16683)
1 parent 9d1e25e commit a264579

File tree

1 file changed

+101
-92
lines changed

1 file changed

+101
-92
lines changed

docs/csharp/programming-guide/types/boxing-and-unboxing.md

Lines changed: 101 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -11,100 +11,109 @@ helpviewer_keywords:
1111
ms.assetid: 8da9bbf4-bce9-4b08-b2e5-f64c11c56514
1212
---
1313
# Boxing and Unboxing (C# Programming Guide)
14-
Boxing is the process of converting a [value type](../../language-reference/keywords/value-types.md) to the type `object` or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a <xref:System.Object?displayProperty=nameWithType> instance and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object.
15-
16-
In the following example, the integer variable `i` is *boxed* and assigned to object `o`.
17-
18-
[!code-csharp[csProgGuideTypes#14](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#14)]
19-
20-
The object `o` can then be unboxed and assigned to integer variable `i`:
21-
22-
[!code-csharp[csProgGuideTypes#15](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#15)]
23-
24-
The following examples illustrate how boxing is used in C#.
25-
26-
[!code-csharp[csProgGuideTypes#47](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#47)]
27-
28-
## Performance
29-
In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally. For more information, see [Performance](../../../framework/performance/performance-tips.md).
30-
31-
## Boxing
32-
Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a [value type](../../language-reference/keywords/value-types.md) to the type `object` or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object.
33-
34-
Consider the following declaration of a value-type variable:
35-
36-
[!code-csharp[csProgGuideTypes#17](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#17)]
37-
38-
The following statement implicitly applies the boxing operation on the variable `i`:
39-
40-
[!code-csharp[csProgGuideTypes#18](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#18)]
41-
42-
The result of this statement is creating an object reference `o`, on the stack, that references a value of the type `int`, on the heap. This value is a copy of the value-type value assigned to the variable `i`. The difference between the two variables, `i` and `o`, is illustrated in the following image of boxing conversion:
43-
44-
![Graphic showing the difference between i and o variables.](./media/boxing-and-unboxing/boxing-operation-i-o-variables.gif)
45-
46-
It is also possible to perform the boxing explicitly as in the following example, but explicit boxing is never required:
47-
48-
[!code-csharp[csProgGuideTypes#19](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#19)]
49-
50-
## Description
51-
This example converts an integer variable `i` to an object `o` by using boxing. Then, the value stored in the variable `i` is changed from `123` to `456`. The example shows that the original value type and the boxed object use separate memory locations, and therefore can store different values.
52-
53-
## Example
54-
[!code-csharp[csProgGuideTypes#16](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#16)]
55-
56-
## Unboxing
57-
Unboxing is an explicit conversion from the type `object` to a [value type](../../language-reference/keywords/value-types.md) or from an interface type to a value type that implements the interface. An unboxing operation consists of:
58-
59-
- Checking the object instance to make sure that it is a boxed value of the given value type.
60-
61-
- Copying the value from the instance into the value-type variable.
62-
63-
The following statements demonstrate both boxing and unboxing operations:
64-
65-
[!code-csharp[csProgGuideTypes#21](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#21)]
66-
67-
The following figure demonstrates the result of the previous statements:
68-
69-
![Graphic showing an unboxing conversion.](./media/boxing-and-unboxing/unboxing-conversion-operation.gif)
70-
71-
For the unboxing of value types to succeed at run time, the item being unboxed must be a reference to an object that was previously created by boxing an instance of that value type. Attempting to unbox `null` causes a <xref:System.NullReferenceException>. Attempting to unbox a reference to an incompatible value type causes an <xref:System.InvalidCastException>.
72-
73-
## Example
74-
The following example demonstrates a case of invalid unboxing and the resulting `InvalidCastException`. Using `try` and `catch`, an error message is displayed when the error occurs.
75-
76-
[!code-csharp[csProgGuideTypes#20](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#20)]
77-
78-
This program outputs:
79-
80-
`Specified cast is not valid. Error: Incorrect unboxing.`
81-
82-
If you change the statement:
83-
14+
15+
Boxing is the process of converting a [value type](../../language-reference/keywords/value-types.md) to the type `object` or to any interface type implemented by this value type. When the common language runtime (CLR) boxes a value type, it wraps the value inside a <xref:System.Object?displayProperty=nameWithType> instance and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object.
16+
17+
In the following example, the integer variable `i` is *boxed* and assigned to object `o`.
18+
19+
[!code-csharp[csProgGuideTypes#14](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#14)]
20+
21+
The object `o` can then be unboxed and assigned to integer variable `i`:
22+
23+
[!code-csharp[csProgGuideTypes#15](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#15)]
24+
25+
The following examples illustrate how boxing is used in C#.
26+
27+
[!code-csharp[csProgGuideTypes#47](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#47)]
28+
29+
## Performance
30+
31+
In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally. For more information, see [Performance](../../../framework/performance/performance-tips.md).
32+
33+
## Boxing
34+
35+
Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a [value type](../../language-reference/keywords/value-types.md) to the type `object` or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object.
36+
37+
Consider the following declaration of a value-type variable:
38+
39+
[!code-csharp[csProgGuideTypes#17](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#17)]
40+
41+
The following statement implicitly applies the boxing operation on the variable `i`:
42+
43+
[!code-csharp[csProgGuideTypes#18](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#18)]
44+
45+
The result of this statement is creating an object reference `o`, on the stack, that references a value of the type `int`, on the heap. This value is a copy of the value-type value assigned to the variable `i`. The difference between the two variables, `i` and `o`, is illustrated in the following image of boxing conversion:
46+
47+
![Graphic showing the difference between i and o variables.](./media/boxing-and-unboxing/boxing-operation-i-o-variables.gif)
48+
49+
It is also possible to perform the boxing explicitly as in the following example, but explicit boxing is never required:
50+
51+
[!code-csharp[csProgGuideTypes#19](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#19)]
52+
53+
## Description
54+
55+
This example converts an integer variable `i` to an object `o` by using boxing. Then, the value stored in the variable `i` is changed from `123` to `456`. The example shows that the original value type and the boxed object use separate memory locations, and therefore can store different values.
56+
57+
## Example
58+
59+
[!code-csharp[csProgGuideTypes#16](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#16)]
60+
61+
## Unboxing
62+
63+
Unboxing is an explicit conversion from the type `object` to a [value type](../../language-reference/keywords/value-types.md) or from an interface type to a value type that implements the interface. An unboxing operation consists of:
64+
65+
- Checking the object instance to make sure that it is a boxed value of the given value type.
66+
67+
- Copying the value from the instance into the value-type variable.
68+
69+
The following statements demonstrate both boxing and unboxing operations:
70+
71+
[!code-csharp[csProgGuideTypes#21](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#21)]
72+
73+
The following figure demonstrates the result of the previous statements:
74+
75+
![Graphic showing an unboxing conversion.](./media/boxing-and-unboxing/unboxing-conversion-operation.gif)
76+
77+
For the unboxing of value types to succeed at run time, the item being unboxed must be a reference to an object that was previously created by boxing an instance of that value type. Attempting to unbox `null` causes a <xref:System.NullReferenceException>. Attempting to unbox a reference to an incompatible value type causes an <xref:System.InvalidCastException>.
78+
79+
## Example
80+
81+
The following example demonstrates a case of invalid unboxing and the resulting `InvalidCastException`. Using `try` and `catch`, an error message is displayed when the error occurs.
82+
83+
[!code-csharp[csProgGuideTypes#20](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#20)]
84+
85+
This program outputs:
86+
87+
`Specified cast is not valid. Error: Incorrect unboxing.`
88+
89+
If you change the statement:
90+
8491
```csharp
85-
int j = (short) o;
86-
```
87-
88-
to:
89-
92+
int j = (short) o;
93+
```
94+
95+
to:
96+
9097
```csharp
91-
int j = (int) o;
92-
```
93-
94-
the conversion will be performed, and you will get the output:
95-
96-
`Unboxing OK.`
97-
98-
## C# Language Specification
99-
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
100-
101-
## Related Sections
102-
For more information:
103-
104-
- [Reference Types](../../language-reference/keywords/reference-types.md)
105-
106-
- [Value Types](../../language-reference/keywords/value-types.md)
107-
98+
int j = (int) o;
99+
```
100+
101+
the conversion will be performed, and you will get the output:
102+
103+
`Unboxing OK.`
104+
105+
## C# language specification
106+
107+
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
108+
109+
## Related sections
110+
111+
For more information:
112+
113+
- [Reference Types](../../language-reference/keywords/reference-types.md)
114+
115+
- [Value Types](../../language-reference/keywords/value-types.md)
116+
108117
## See also
109118

110119
- [C# Programming Guide](../index.md)

0 commit comments

Comments
 (0)