区别与QUOT;管理"和"非托管"区别、QUOT

2023-09-02 01:24:35 作者:兄弟不是说说而已

我听到/有时当谈到.NET,例如管理code和非托管code读到它,但我不知道它们是什么,什么是他们之间的分歧。是他们的区别,顾名思义?什么是使用其中一方的后果是什么?请问这种区别存在于.NET / Windows的唯一?

I hear/read about it sometimes when talking about .NET, for example "managed code" and "unmanaged code" but I have no idea what they are and what are their differences. What are their difference, by definition? What are the consequences of using either of them? Does this distinction exist in .NET/Windows only?

推荐答案

管理code是Visual Basic .NET和C#编译器创建。它运行在CLR(公共语言运行库),其中,除其他事项外,提供服务,如垃圾收集,运行时类型检查和背景调查。因此,认为它是我的code为的管理的由CLR。

Managed Code

Managed code is what Visual Basic .NET and C# compilers create. It runs on the CLR (Common Language Runtime), which, among other things, offers services like garbage collection, run-time type checking, and reference checking. So, think of it as, "My code is managed by the CLR."

Visual Basic和C#可以的只有的产生管理code,所以,如果你正在写的应用程序在你正在编写由CLR管理的应用程序的语言之一。如果你正在写在Visual C ++ .NET可以生成管理code,如果你喜欢的应用程序,但它是可选的。

Visual Basic and C# can only produce managed code, so, if you're writing an application in one of those languages you are writing an application managed by the CLR. If you are writing an application in Visual C++ .NET you can produce managed code if you like, but it's optional.

未管理code编译直机code。因此,根据这一定义由传统的C / C ++编译器编译所有code是'非托管code'。另外,由于它编译成机器code和未处理的中间语言它是非便携式的。

Unmanaged code compiles straight to machine code. So, by that definition all code compiled by traditional C/C++ compilers is 'unmanaged code'. Also, since it compiles to machine code and not an intermediate language it is non-portable.

没有免费的内存管理或其他任何CLR提供。

No free memory management or anything else the CLR provides.

既然你不能创建非托管code使用Visual Basic和C#,Visual Studio中的所有非托管code是用C / C ++。

Since you cannot create unmanaged code with Visual Basic or C#, in Visual Studio all unmanaged code is written in C/C++.

由于Visual C ++可以被编译成或者托管或非托管code,可以混合使用这两种在同一应用程序。这模糊了两者之间的线,复杂的定义,但它是值得一提的只是让你知道,你仍然可以有内存泄漏,举个例子,你在使用第三方库的一些写得很差的非托管code。

Since Visual C++ can be compiled to either managed or unmanaged code it is possible to mix the two in the same application. This blurs the line between the two and complicates the definition, but it's worth mentioning just so you know that you can still have memory leaks if, for example, you're using a third party library with some badly written unmanaged code.

下面是一个例子,我发现谷歌上搜索:

Here's an example I found by googling:

#using <mscorlib.dll>
using namespace System;

#include "stdio.h"

void ManagedFunction()
{
    printf("Hello, I'm managed in this sectionn");
}

#pragma unmanaged
UnmanagedFunction()
{
    printf("Hello, I am unmanaged through the wonder of IJW!n");
    ManagedFunction();
}

#pragma managed
int main()
{
    UnmanagedFunction();
    return 0;
}