The #using Directive

#using can be considered equivalent to the import statement and is used to refer to the .NET libraries. For example, to use some of the .NET framework classes in your C++ code, you first employ the following:

#using <mscorlib.dll>

This directive should be followed by the declaration for exactly which namespaces you need.

Listing 1.4.1 shows the creation of a simple application containing a GC class.

Listing 1.4.1. managed.cpp: A First Look at Garbage Collected C++
#using <mscorlib.dll> // import the library
using namespace System;

__gc class MCPPDemo
{
public:
    void SayHello()
    {
        Console::WriteLine("Hello Managed C++");
    }
};

void main()
{
    MCPPDemo *pD=new MCPPDemo;
    pD->SayHello();
}

There are a few things in this code ...

Get C# and the .NET Framework: The C++ Perspective now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.