Last Updated:

Can we use C++ in .NET Core? - Visual C++ .NET

In each new version of Visual C++, Microsoft expands the language in many ways. Visual C++ .NET is no exception, supporting many new features for which new keywords and attributes have been introduced. In particular, there is support for developing code in managed C++ for the .NET platform. This chapter provides several examples to help you become familiar with the core classes of the .NET Framework and start writing code in managed C++ yourself.

C++ in .NET Core

Using the Console class as an example, standard input and output are demonstrated, and the extremely useful String and Array classes are also considered. The following is a program for managing the hotel reservation system, which we will return to more than once in the next chapters. It then discusses important aspects of managed C++ programming to generate code for the .NET platform: using managed, unmanaged, value, and abstract types, interfaces, packaging and decompression, delegates, events, properties, and managed exception handlers. Finally, the C++ attributes in the context of creating ATL COM projects are discussed.

One of the advantages of the .NET framework is that you can use any of a wide range of languages to develop .NET-based applications, components, and services. You can use C++ with manageability, C# and VB.NET created by Microsoft, as well as many other languages developed by other companies. But the main thing is not even that you can use all these languages to create applications based on .NET, but that in all matters related to object initialization, method calling, inheritance, event handling, and even exception handling, the application will not depend on the implementation languages of its components. This is made possible by the fact that .NET languages are compiled not into the native language, but into a common intermediate language Intermediate Language (IL).

As discussed in the previous chapters, code that runs under the Common Language Runtime (CLR) is called managed code. Managed code differs from regular code in that it is not compiled into a native set of CPU instructions, but into an instruction in an intermediate IL defined by the .NET framework. The intermediate IL language is similar to the usual set of CPU instructions, differing from the latter in that it was originally developed with support for object-oriented and component-oriented features common to languages, such as classes, objects, methods, events, and exceptions. Because source code written in languages that support .NET is compiled into intermediate IL instructions, all of these languages are fully compatible.

Programs consist of code and data, and the common language runtime (CLR) provides support for both managed data and managed code. As mentioned in the previous chapters, managed data is placed in a managed dynamically allocated memory area (heap), which has a very valuable feature - automatic garbage collection. While a programmer must create dynamically allocated memory controls when creating programs in plain C++, the common language runtime (CLR) implements this process by tracking object references and automatically freeing up resources that are no longer available to the program.

By using C++ with Manageability Extensions, you can generate managed code and data. However, C++ is currently the only .NET language that you can use to also generate unmanaged code and data. In fact, managed and unmanaged C++ code and data can be defined in the same source file, and to some extent the two worlds can interact. While using managed code and data has many benefits, it can lead to poor performance and loss of flexibility. Therefore, in many cases, C++ is the best choice for creating programs. Another reason to choose C++ from all .NET languages may be the desire to improve your knowledge of C++ and existing developments in this language.