This note contains the following content:
1. How to enable CLR support in VC ++ 2005 or 2008 2. A simple hello WorldProgram 3. Reference Type and Value Type 4. Definitions of common CLI types 5. array in CLI (CLI: array) 6. Unified System Unification) 1. How to enable CLR support in VC ++ 2005 or 2008 In the current solution browser, select solution, right-click, select property, in the pop-up dialog box, select the common language runtime support (/CLR) option under the general menu of configuration properties and you will be able to use it normally. net content 2. A simple hello World Program Using Namespace System
IntMain ()
{
System: Console: writeline ("Hello, world");
}3. Reference Type and Value Type Using NamespaceSystem;
ref class class1
{< br> Public :< br> int value;
class1 ()
{< br> value = 0 ;< BR >}
int main ()
{< br> int val1 = 0 ;
int val2 = val1;
val2 = 123 ;
Class1^Ref1=Gcnew class1;
Class1^Ref2=Ref1;
Ref2->Value= 123;
Console: writeline ( " Values: {0}, {1} " , Val1, val2 );
Console: writeline ( " Refs: {0}, {1} " , Ref1 -> Value, ref2 -> Value );
} Note: 1) ^ indicates that this type is managed. 2) the difference between gcnew and new is that gcnew creates an object on the managed stack and can implement automatic garbage collection. 3) ref indicates that this type is a hosted reference type. 4. Definitions of common CLI types Public Enum Class Color
{
Red, blue, green
};
PublicValueStructPoint
{
IntX, Y;
};
Public Interface ClassIBase
{
VoidF ();
};
Public Interface ClassIderived: IBASE
{
VoidG ();
};
Public ref class A
{< br> protected :< br> virtual void H ()
{< br> Console: writeline ( " . h " );
}< BR >};
Public ref class B:, iderived
{< br> Public :
virtual void F ()
{< BR >}
virtual void G ()
{< BR >}< br> protected :< br> virtual void H () override
{< BR >}< BR >};
Public Delegate VoidMydelegate ();Includes definitions of common types5. array in CLI (CLI: array) Using NamespaceSystem;
Int Main ()
{
Array < Int > ^ Arr1d = Gcnew Array < Int > ( 4 ){ 10 , 42 , 30 , 12 };
Console: Write ( " The {0} elements are: " , Arr1d -> Length );
For(IntI= 0; I<Arr1d. length; I++)
{
Console: Write ("{0, 3}", I );
}
Console: writeline ();
Array< Int , 3 > ^ Arr3d = Gcnew Array < Int , 3 > ( 10 , 20 , 30 );
} Note: 1) array <int> ^ indicates that this is a hosted array. 2) array can be a multi-dimensional array, which is specified when the array is created, for example, arr3d is an array of 10*20*30. 6. Unified types of systems Using Namespace System;
IntMain ()
{
Console: writeline ((3). Tostring ());
Int I = 123 ;
Object ^ O = I; // Boxing
Int J = Safe_cast < Int > (O ); // Unboxing
Console: writeline ( " {0} " , J );
} Note: Both Java and C # use the same type of system, that is, all types are integrated from the object type. the traditional C ++ is not like this. In CLI, all types are integrated from system: object. in the above program, Number 3 also has the tostring () method. another point is the packing and unpacking operations. In CLI, you can directly assign the value type to the reference type. it is called the packing operation. Similarly, the safe_cast method can be called to convert the reference type to the value type to become the packing operation. these operations are time-consuming. Please indicate the source. If you have any questions, please kindly advise.