C# 究竟什么是“托管”代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/57923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:14:45  来源:igfitidea点击:

What exactly is "managed" code?

提问by Graeme Perrow

I've been writing C / C++ code for almost twenty years, and I know Perl, Python, PHP, and some Java as well, and I'm teaching myself JavaScript. But I've never done any .NET, VB, or C# stuff. What exactly does managedcode mean?

我已经编写 C/C++ 代码将近 20 年了,我知道 Perl、Python、PHP 和一些 Java,而且我正在自学 JavaScript。但我从来没有做过任何 .NET、VB 或 C# 的东西。托管代码究竟是什么意思?

Wikipedia describes itsimply as

维基百科将其简单地描述

Code that executes under the management of a virtual machine

在虚拟机管理下执行的代码

and it specifically says that Java is (usually) managed code, so

它特别说明 Java 是(通常)托管代码,所以

  • why does the term only seem to apply to C# / .NET?
  • Can you compile C# into a .exe that contains the VM as well, or do you have to package it up and give it to another .exe (a la java)?
  • 为什么这个术语似乎只适用于 C#/.NET?
  • 您能否将 C# 编译成包含 VM 的 .exe,还是必须将其打包并提供给另一个 .exe(a la java)?

In a similar vein,

与此相类似,

  • is .NET a languageor a framework, and what exactly does "framework" mean here?
  • .NET 是一种语言还是一种框架,这里的“框架”究竟是什么意思?

OK, so that's more than one question, but for someone who's been in the industry as long as I have, I'm feeling rather N00B-ish right now...

好的,所以这不仅仅是一个问题,但是对于和我一样在这个行业工作了很长时间的人来说,我现在感觉相当 N00B-ish...

采纳答案by lnediger

When you compile C# code to a .exe, it is compiled to Common Intermediate Language(CIL) bytecode. Whenever you run a CIL executable it is executed on Microsofts Common Language Runtime(CLR) virtual machine. So no, it is not possible to include the VM withing your .NET executable file. You must have the .NET runtime installed on any client machines where your program will be running.

将 C# 代码编译为 .exe 时,它​​会编译为通用中间语言 (CIL) 字节码。每当您运行 CIL 可执行文件时,它都会在 Microsoft 的公共语言运行时 (CLR) 虚拟机上执行。所以不,不可能在 .NET 可执行文件中包含 VM。您必须在将运行您的程序的任何客户端计算机上安装 .NET 运行时。

To answer your second question, .NET is a framework, in that it is a set of libraries, compilers and VM that is not language specific. So you can code on the .NET framework in C#, VB, C++ and any other languages which have a .NET compiler.

要回答您的第二个问题,.NET 是一个框架,因为它是一组非特定于语言的库、编译器和 VM。因此,您可以使用 C#、VB、C++ 和任何其他具有 .NET 编译器的语言在 .NET 框架上进行编码。

https://bitbucket.org/brianritchie/wiki/wiki/.NET%20Languages

https://bitbucket.org/brianritchie/wiki/wiki/.NET%20Languages

The above page has a listing of languages which have .NET versions, as well as links to their pages.

上面的页面列出了具有 .NET 版本的语言,以及指向它们页面的链接。

回答by GEOCHET

.NET is a framework. It can be used from many languages (VB.NET, C#, IronPython, boo, etc)

.NET 是一个框架。它可以用于多种语言(VB.NET、C#、IronPython、boo 等)

.NET always executes as interpreted, and no you cannot include the 'VM' inside the .exe. Any user wishing to run your .NET app must have the framework installed.

.NET 始终按解释执行,并且您不能在 .exe 中包含“VM”。任何希望运行您的 .NET 应用程序的用户都必须安装该框架。

回答by Kristopher Johnson

The term managedis generally applied only to .NET because Microsoft uses the term. Microsoft generally doesn't use the term "virtual machine" in reference to a .NET managed execution environment.

术语托管通常仅适用于 .NET,因为 Microsoft 使用该术语。Microsoft 通常不使用术语“虚拟机”来指代 .NET 托管执行环境。

.NET's "bytecode" (IL) is somewhat different from Java bytecode in that it was explicitly designed to be compiled into native code prior to execution in the managed environment, whereas Java was designed to be interpreted, but the concept of platform-independent code is similar.

.NET 的“字节码”(IL)与 Java 字节码有些不同,因为它被明确设计为在托管环境中执行之前被编译为本机代码,而 Java 被设计为可解释的,但与平台无关的代码的概念类似。

The ".NET Framework" is basically a huge set of libraries provided by Microsoft, containing thousands of classes that can be used to develop applications.

“.NET Framework”基本上是由Microsoft 提供的一组庞大的库,其中包含可用于开发应用程序的数千个类。

A compiled C# .exe contains platform-independent code that can be run in any .NET-compatible environment, including Mono. However, the runtime is generally distributed separately from applications that use it.

编译后的 C# .exe 包含独立于平台的代码,可以在任何与 .NET 兼容的环境中运行,包括 Mono。但是,运行时通常与使用它的应用程序分开分发。

回答by Chris Upchurch

It's primarily used to describe .NET because that's the term Microsoft chose to differentiate .NET from C/C++ and other older languages. Microsoft chose it because it wasn't a term that was usually associated with Java because they didn't want to emphasize the similarities between C#/.NET and Java (as opposed to calling it something like 'virtual machine code' which would make it sound much more Java like). Basically, the use of "managed code" is marketing driven, rather than technically driven, terminology.

它主要用于描述 .NET,因为这是 Microsoft 选择将 .NET 与 C/C++ 和其他旧语言区分开来的术语。微软选择它是因为它不是一个通常与 Java 相关的术语,因为他们不想强调 C#/.NET 和 Java 之间的相似性(而不是将它称为“虚拟机代码”之类的东西,这将使听起来更像Java)。基本上,“托管代码”的使用是营销驱动的,而不是技术驱动的术语。

回答by Ed S.

.NET is a framework. The Common language Runtime (CLR) executes the Microsoft Intermediate Language (MSIL) code that is generated when a solution is compiled (i.e., it does not compile to machine code). You cannot contain the API within the exe, nor would you want to as it is quite large. The major benefit here is memory management (among some other security advantages and possibly others that I do not know about.)

.NET 是一个框架。公共语言运行时 (CLR) 执行编译解决方案时生成的 Microsoft 中间语言 (MSIL) 代码(即,它不会编译为机器代码)。您不能在 exe 中包含 API,也不希望包含它,因为它非常大。这里的主要好处是内存管理(以及其他一些安全优势和我不知道的其他优势。)

回答by Glenn Slaven

I can answer the framework question. .NET is a framework, C#, VB.NET, etc are languages. Basically .NET provides a common platform of libraries to call (All the System.... dlls) that any language using .NET can call. All the .NET languages are compiled into MSIL (Microsoft Intermediate Language, better known as just IL) which can then be run on any PC with the appropriate .NET framework installed.

我可以回答框架问题。.NET 是一个框架,C#、VB.NET 等是语言。基本上.NET 提供了一个通用的库平台来调用(所有系统....dll),任何使用.NET 的语言都可以调用它。所有的 .NET 语言都被编译成 MSIL(微软中间语言,简称 IL),然后可以在任何安装了适当的 .NET 框架的 PC 上运行。

回答by jodonnell

Managed means that the code is not compiled to native code, and thus runs under the auspices of a virtual machine. Java compiles to an intermediate format called bytecode, which the Java VM knows how to interpret and execute. All the .NET languages do a similar thing, compiling to IL (intermediate language) which the .NET runtime interprets. It's a little confusing because the .NET IL have .dll and .exe file endings.

托管意味着代码不会编译为本机代码,因此在虚拟机的支持下运行。Java 编译为称为字节码的中间格式,Java VM 知道如何解释和执行字节码。所有 .NET 语言都做类似的事情,编译为 .NET 运行时解释的 IL(中间语言)。这有点令人困惑,因为 .NET IL 具有 .dll 和 .exe 文件结尾。

回答by Vagnerr

Mostly its referring to the fact that all of your memory allocations are "managed" for you. If you are using managed code you don't have to worry about freeing your objects when you are done with them. Simply allowing them to go out of scope will mean that the VM will eventually recognize that there are no longer any references to them and will Garbage collect them returning the memory to the system.

主要是指您的所有内存分配都是为您“管理”的。如果您使用的是托管代码,则不必担心在使用完对象后释放它们。简单地允许它们超出范围将意味着 VM 最终将识别出不再有任何对它们的引用,并将垃圾收集它们并将内存返回给系统。

Unmanaged code on the other hand will simply "leak" unless you explicitly free your pointers before you discard the references.

另一方面,非托管代码只会“泄漏”,除非您在丢弃引用之前明确释放指针。

回答by lnediger

It can refer to any code executed by a virtual machine rather than directly by the CPU.

它可以指由虚拟机执行的任何代码,而不是直接由 CPU 执行的代码。

I think this enables things like garbage collection and array bounds checking.

我认为这可以实现垃圾收集和数组边界检查之类的功能。

回答by David L Morris

At the risk of offending some, I suspect that the word managed was used so they could use the word unmanaged instead of compiled. While managed may mean more, the reality is it seems to be the used to distinguish mostly between what is pretty much just in time compiling (as the replacement for what was one once interpreted or pcode) and native compiled code.

冒着冒犯某些人的风险,我怀疑使用了托管一词,以便他们可以使用非托管一词而不是编译。虽然托管可能意味着更多,但实际情况是它似乎主要用于区分几乎及时编译的代码(作为曾经解释过的代码或 pcode 的替代品)和本机编译的代码。

Or put another way, which would you prefer to use:

或者换一种方式,您更喜欢使用哪种方式:

a) Unmanaged code that may do uncontrollable things to the system.

a) 非托管代码可能会对系统造成无法控制的事情。

b) Native compiled code that is fast, solid and is close to the OS.

b) 快速、可靠且接近操作系统的本地编译代码。

Of course, they are actually the same thing.

当然,它们实际上是一回事。