Skip to content

Commit aeb0873

Browse files
author
Colin Robertson
committed
Fix Markdig issues in dotnet
1 parent 5245f33 commit aeb0873

File tree

226 files changed

+60033
-57525
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+60033
-57525
lines changed

docs/dotnet/a-tracking-handle-to-a-boxed-value.md

Lines changed: 81 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -12,82 +12,84 @@ ms.author: "mblome"
1212
ms.workload: ["cplusplus", "dotnet"]
1313
---
1414
# A Tracking Handle to a Boxed Value
15-
The usage of a tracking handle to reference a value type has changed from Managed Extensions for C++ to Visual C++.
16-
17-
Boxing is a peculiarity of the CLR unified type system. Value types directly contain their state, while reference types are an implicit pair: the named entity is a handle to an unnamed object allocated on the managed heap. Any initialization or assignment of a value type to an `Object`, for example, requires that the value type be placed within the CLR heap - this is where the image of boxing it arises - first by allocating the associated memory, then by copying the value type’s state, and then returning the address of this anonymous Value/Reference hybrid. Thus, when one writes in C#
18-
19-
```
20-
object o = 1024; // C# implicit boxing
21-
```
22-
23-
there is a great deal more going on than is made apparent by the simplicity of the code. The design of C# hides the complexity not only of what operations are taking place under the hood, but also of the abstraction of boxing itself. Managed Extensions for C++, on the other hand, concerned that this would lead to a false sense of efficiency, puts it in the user’s face by requiring an explicit instruction:
24-
25-
```
26-
Object *o = __box( 1024 ); // Managed Extensions explicit boxing
27-
```
28-
29-
Boxing is implicit in Visual C++:
30-
31-
```
32-
Object ^o = 1024; // new syntax implicit boxing
33-
```
34-
35-
The `__box` keyword serves a vital service within Managed Extensions, one that is absent by design from languages such as C# and Visual Basic: it provides both a vocabulary and tracking handle for directly manipulating a boxed instance on the managed heap. For example, consider the following small program:
36-
37-
```
38-
int main() {
39-
double result = 3.14159;
40-
__box double * br = __box( result );
41-
42-
result = 2.7;
43-
*br = 2.17;
44-
Object * o = br;
45-
46-
Console::WriteLine( S"result :: {0}", result.ToString() ) ;
47-
Console::WriteLine( S"result :: {0}", __box(result) ) ;
48-
Console::WriteLine( S"result :: {0}", br );
49-
}
50-
```
51-
52-
The underlying code generated for the three invocations of `WriteLine` show the various costs of accessing the value of a boxed value type (thanks to Yves Dolce for pointing out these differences), where the indicated lines show the overhead associated with each invocation.
53-
54-
```
55-
// Console::WriteLine( S"result :: {0}", result.ToString() ) ;
56-
ldstr "result :: {0}"
57-
ldloca.s result // ToString overhead
58-
call instance string  [mscorlib]System.Double::ToString() // ToString overhead
59-
call void [mscorlib]System.Console::WriteLine(string, object)
60-
61-
// Console::WriteLine( S"result :: {0}", __box(result) ) ;
62-
Ldstr " result :: {0}"
63-
ldloc.0
64-
box [mscorlib]System.Double // box overhead
65-
call void [mscorlib]System.Console::WriteLine(string, object)
66-
67-
// Console::WriteLine( S"result :: {0}", br );
68-
ldstr "result :: {0}"
69-
ldloc.0
70-
call void [mscorlib]System.Console::WriteLine(string, object)
71-
```
72-
73-
Passing the boxed value type directly to `Console::WriteLine` eliminates both the boxing and the need to invoke `ToString()`. (Of course, there is the earlier boxing to initialize `br`, so we don’t gain anything unless we really put `br` to work.
74-
75-
In the new syntax, the support for boxed value types is considerably more elegant and integrated within the type system while retaining its power. For example, here is the translation of the earlier small program:
76-
77-
```
78-
int main()
79-
{
80-
double result = 3.14159;
81-
double^ br = result;
82-
result = 2.7;
83-
*br = 2.17;
84-
Object^ o = br;
85-
Console::WriteLine( "result :: {0}", result.ToString() );
86-
Console::WriteLine( "result :: {0}", result );
87-
Console::WriteLine( "result :: {0}", br );
88-
}
89-
```
90-
91-
## See Also
92-
[Value Types and Their Behaviors (C++/CLI)](../dotnet/value-types-and-their-behaviors-cpp-cli.md)
93-
[How to: Explicitly Request Boxing](../dotnet/how-to-explicitly-request-boxing.md)
15+
16+
The usage of a tracking handle to reference a value type has changed from Managed Extensions for C++ to Visual C++.
17+
18+
Boxing is a peculiarity of the CLR unified type system. Value types directly contain their state, while reference types are an implicit pair: the named entity is a handle to an unnamed object allocated on the managed heap. Any initialization or assignment of a value type to an `Object`, for example, requires that the value type be placed within the CLR heap - this is where the image of boxing it arises - first by allocating the associated memory, then by copying the value type’s state, and then returning the address of this anonymous Value/Reference hybrid. Thus, when one writes in C#
19+
20+
```cpp
21+
object o = 1024; // C# implicit boxing
22+
```
23+
24+
there is a great deal more going on than is made apparent by the simplicity of the code. The design of C# hides the complexity not only of what operations are taking place under the hood, but also of the abstraction of boxing itself. Managed Extensions for C++, on the other hand, concerned that this would lead to a false sense of efficiency, puts it in the user’s face by requiring an explicit instruction:
25+
26+
```cpp
27+
Object *o = __box( 1024 ); // Managed Extensions explicit boxing
28+
```
29+
30+
Boxing is implicit in Visual C++:
31+
32+
```cpp
33+
Object ^o = 1024; // new syntax implicit boxing
34+
```
35+
36+
The `__box` keyword serves a vital service within Managed Extensions, one that is absent by design from languages such as C# and Visual Basic: it provides both a vocabulary and tracking handle for directly manipulating a boxed instance on the managed heap. For example, consider the following small program:
37+
38+
```cpp
39+
int main() {
40+
double result = 3.14159;
41+
__box double * br = __box( result );
42+
43+
result = 2.7;
44+
*br = 2.17;
45+
Object * o = br;
46+
47+
Console::WriteLine( S"result :: {0}", result.ToString() ) ;
48+
Console::WriteLine( S"result :: {0}", __box(result) ) ;
49+
Console::WriteLine( S"result :: {0}", br );
50+
}
51+
```
52+
53+
The underlying code generated for the three invocations of `WriteLine` show the various costs of accessing the value of a boxed value type (thanks to Yves Dolce for pointing out these differences), where the indicated lines show the overhead associated with each invocation.
54+
55+
```cpp
56+
// Console::WriteLine( S"result :: {0}", result.ToString() ) ;
57+
ldstr "result :: {0}"
58+
ldloca.s result // ToString overhead
59+
call instance string  [mscorlib]System.Double::ToString() // ToString overhead
60+
call void [mscorlib]System.Console::WriteLine(string, object)
61+
62+
// Console::WriteLine( S"result :: {0}", __box(result) ) ;
63+
Ldstr " result :: {0}"
64+
ldloc.0
65+
box [mscorlib]System.Double // box overhead
66+
call void [mscorlib]System.Console::WriteLine(string, object)
67+
68+
// Console::WriteLine( S"result :: {0}", br );
69+
ldstr "result :: {0}"
70+
ldloc.0
71+
call void [mscorlib]System.Console::WriteLine(string, object)
72+
```
73+
74+
Passing the boxed value type directly to `Console::WriteLine` eliminates both the boxing and the need to invoke `ToString()`. (Of course, there is the earlier boxing to initialize `br`, so we don’t gain anything unless we really put `br` to work.
75+
76+
In the new syntax, the support for boxed value types is considerably more elegant and integrated within the type system while retaining its power. For example, here is the translation of the earlier small program:
77+
78+
```cpp
79+
int main()
80+
{
81+
double result = 3.14159;
82+
double^ br = result;
83+
result = 2.7;
84+
*br = 2.17;
85+
Object^ o = br;
86+
Console::WriteLine( "result :: {0}", result.ToString() );
87+
Console::WriteLine( "result :: {0}", result );
88+
Console::WriteLine( "result :: {0}", br );
89+
}
90+
```
91+
92+
## See Also
93+
94+
[Value Types and Their Behaviors (C++/CLI)](../dotnet/value-types-and-their-behaviors-cpp-cli.md)<br/>
95+
[How to: Explicitly Request Boxing](../dotnet/how-to-explicitly-request-boxing.md)

0 commit comments

Comments
 (0)