USING Managed Reference Types in C by Chris Maunder a Quick Introduction To use .NET Managed Reference Types in C
Introduction
One of the nice things about .NET are the common base classes. These classes are essentially the API of .NET, and are available from all languages equally. Once you know how to use a String in VB.NET you also know how to use IT IN C # and C . Once You Have For One Language You Can The Go ON To Use That Knowledge In Other .net languages.
In .NET there are value types and reference types. Value types refer to simple data structures such as int's and enumerations, and are stored on the stack. Reference types are created on the .NET managed heap, which allows the garbage collector to track their Lifetime and Free Instances When The isy is not no longer required.
Think of a reference type as a pointer - though not in the traditional C / C sense of the word The actual location of a variable on the managed heap will change as the garbage collector recovers unused memory and compacts the heap, so in a short. time a traditional pointer to a spot on the heap will be invalid. A .NET reference, on the other hand, will always give you access to your values no matter where it has been moved on the heap. A variable of reference type will always Either Contain A Reference To a value of what type, or null. Assigning The value of a reference variable to another variable copies the reference, not the value stored. be warned!
Value types are stored on the stack and are accessed directly. Once the memory containing that value is freed, the value type instance is destroyed. Hence, references to value types are not allowed. If it were allowed it would be possible to have a reference point to an invalid memory location. A value type will always point to a variable of that type, and can not be null. Assigning a value type to another variable results in a copy of the value being made.Creating an instance of a reference type
Value types are easy since they are declared on the stack. It would be insane if you had to call new each time you wanted to create an int or double. Reference types are a little more complicated in that they can not be created on the stack.
Reference types are created on the .NET managed heap, and so must be created using the overloaded new operator. The new operator for managed types not only creates the object on the managed heap, but also initialises the value of the variable to 0. The value passed back from new will not be a .NET reference, and not pointer in the traditional sense. in the following examples we will concentrate on the String class. You'll use it a lot, and it has some tricks up its sleave.
To create an instance of a reference type you Simply Declare a Pointer of the Variables Type and create the object using new.
String * S = New String ("this is a string");
Attempting to Declare a Managed Object On The Stack SIMPLY WON 'T
String s = "this is a string"; // Will Not Compile
The String Class' Constructor Contains Many Different Overrides for Many Different Occasions, But Does Not Contain An Override for String ().
Other Ways of Declaring A String Are As Follows:
String * s = new string ("this is an affi string"); string * s = "this is an ANSI STRING";
String * s = l "this is a unicode string";
String * s = s "this is a .net string";
ANSI and UNICODE strings should be familiar to you. .NET strings (those prefixed by 'S') are new and offer better performance than standard C literal strings. As well as this, all instances of identical string literals actually point to the same string IF S1 and S2 Are Two String's, THE FOLLOWING CODE:
S1 = s "this is a .net string";
S2 = s "this is a .net string";
IF (S1 == S2)
Printf ("S1 == S2 / N");
Else
Printf ("S1! = S2 / N");
S1 = "THIS IS A C Literal String";
S2 = "THIS IS A C Literal String";
IF (S1 == S2)
Printf ("S1 == S2 / N");
Else
Printf ("S1! = S2 / N");
Would Produce
S1 == S2
S1! = S2
Note That C Literal Strings Can Be Used WHERE Ever .NET STRINGS Are Used, but .NET STRINGS CANNOT BE Used Where C Strings Are Expected.
Note Also The Use of Printf in The Above Snippet. Just Becauses We are uses .NET TYPES AND METHODS DOESN '' We Libraries. In Managed C We get The Best of Both worlds.
Creating Your Own Managed Types
Creating Your Own Managed Types is achieved using the new __gc keyword.
__GC Class Myclass
{
PUBLIC:
Int ID;
}
You the use this class as you would any other managed class:
Myclass * mc = new myclass;
MC-> ID = 5;
BECAUSE MC IS A Managed Type It Will Be Automatically InitialId TO 0 (IE MC-> ID WILL BE SET AS 0).
Using management Types in non-managed functions
.
TO Allow this, a new keyword __pin HAS been Introducesd That Essential So That The Garbage Collector Will Not Move IT.
Myclass __pin * pmc = mc;
Printf ("THE PINNED VALUE OF MC IS% D / N", PMC-> ID);
History
16 OCT 2001 - Updated Source Files for vs.Net Beta 2
About Chris Maunder
Site Admin Editor Site BuilderChris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs CodeProject. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist , defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C MVP both globally and for Canada locally. His programming experience includes C / C , C #, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.Chris was born and bred in Australia but is currently based in Toronto enjoying the weather. For relaxation he is into snowboarding, rock climbing, mountain biking, and storm chasing.Click here to view Chris Maunder ' S Online Profile.