Common Type System—Managed Pointers


Jump to: navigation, search
Visual C# Tutorials
.NET Framework Tutorials

Common Type System

© 2006 Wiley Publishing Inc.

Managed Pointers

Pass-by-reference and output parameters in C# all work implicitly by using managed pointers. Amanaged pointer is used to point to the interior of an instance, either object or value, unlike a normal reference, which points to the beginning of an object record. We say the type of a managed pointer is T&, where T is the type of the data it points to. Managed pointers enable you to refer to an instance’s field, an argument, or data on the execution stack, for example, without worrying about whether it will move. The GC updates managed pointers with new locations whenever it moves instances. And verification ensures that a managed pointer can never outlive the data to which it points.

When you pass something in C# using the ref or out keyword, you’re implicitly creating a new managed pointer and passing that to the method, using one of the load address instructions, that is ldflda, ldarga, ldloca, or ldelema. Accessing the data behind a managed pointer leads to an extra level of indirection, as the execution engine must dereference the managed pointer before it can get at the underlying value or reference.


Previous_Page_.gif Next_Page_.gif





Personal tools