C++/CLI (not Beta 1) allows declaring reference types as local variables. Let's start with this simple ref class:
ref class Test
Handles and the gcnew operator can be used to create instances:
Test^ t = gcnew Test();
And without using a handle - this looks like a value type (this is new with Visual Studio 2005 versions after Beta 1):
What's the difference with the generated IL code? Nothing! Using a reference type as a local variable creates an object on the heap the same way as the version with the local variable. A reference type is not put on the stack.
A special advantage using local variables is with classes that implement IDisposable as you can read in my next weblog entry.