Is Unmanaged C # code compiling in IL and running on the CLR?

In the midst of the issue of manual CLR memory management, I realized that I know very little.

I know that the CLR will put a cookie on the stack when exiting a managed context so that the garbage collector does not trample your memory space; however, in everything I read, it is assumed that you are calling a library written in C.

I want the whole write level of my application in C # outside the managed context to process data at a low level. Then I want to access this layer from a managed level.

In this case, my unmanaged C # code compiles to IL and will run on the CLR? How it works?

+8
c # clr unmanaged
source share
2 answers

I assume this is due to the same C # database project that you mentioned in the question.

It is technically possible to implement entire layers of recording in C / C ++ or in any other language. And technically it is possible to have everything else in C #. I'm currently working on an application that uses unmanaged code for some high-performance low-level materials and C # for business logic and top-level management.

However, the complexity of the task should not be underestimated . A typical way to do this is to develop a contract that both parties can understand. The contract will run in a managed language, and the managed language will cause calls for its own application. If you've ever tried calling a C ++ method from C #, you get this idea ... Plus, every call to unmanaged code has a pretty significant overhead, which can lead to the loss of the whole idea of โ€‹โ€‹poor performance.

If you are really interested in high-performance relational databases, use one low-level language.

If you want to have a naive but fully operational database implementation, just use C #. Do not mix these two languages โ€‹โ€‹if you do not fully understand the complexity. See Raven DB, a document-based NoSQL database built entirely in C # only.

Will my unmanaged C # code compile in IL and run on the CLR?

No, there is no such thing as unmanaged C #. C # code will always be compiled into IL code and executed using the CLR. This is a case of managed code that calls unmanaged code. Unmanaged code can be implemented in several C / C ++ / Assembly languages, etc., but the CLR will not know what is happening in this code.

Update from the comment. There is a tool (ngen.exe) that can compile C # directly into its own architecture code. This tool is designed to improve the performance of a managed application by removing the JIT compilation step and translating native code directly into an executable image or library. However, this code is still โ€œmanagedโ€ using CLR memory: memory allocation and collection, controlled streaming, application areas, exception handling, security, and all other aspects are still controlled by the CLR. Thus, although C # can technically be compiled into native code, this code does not work as a standalone native image.

How it works?

Managed code interacts with unmanaged code. There are several ways to do this:

  • Via code through .Net Interop. It's relatively fast, but it looks a bit ugly in code (plus it's hard to maintain / test) ( good article with C # / C / Assembly samples )
  • A much slower approach, but more open to other languages: web services (SOAP, WS, REST and company), priority (for example, MSMQ, NServiceBus, etc.), and also (possibly) interprocess communication. Thus, an unmanaged process is on one end, and a managed application is on the other.
+12
source share

I know this is a C # question, but if you are comfortable with C ++, C ++ / CLI might be considered an option.

It allows you to selectively compile parts of your C ++ code into a managed or unmanaged context. However, keep in mind that code that interacts with CLR types must execute in a controlled context.

I donโ€™t know about the speed of transition from a managed context to an unmanaged context and vice versa from C ++ code, but I assume that it should be similar to the cost of calling your own method via .net Interop from C #, which, as @oleksii already pointed out , it's expensive. In my experience, it really paid off if you often need to interact with native C or C ++ libraries - IMHO it is much easier to name them from the C ++ / CLI project, rather than writing the necessary .net Interop interfaces in C #.

See question for information on how to do this.

+1
source share

All Articles