什麼是 Managed 程式碼？What is "managed code"?
使用 .NET Framework 時，您經常會遇到「Managed 程式碼」一詞。When working with .NET Framework, you will often encounter the term "managed code". 本文件將說明此詞彙所代表的意義及其他相關資訊。This document will explain what this term means and additional information around it.
簡單來說，Managed 程式碼就是其執行受到執行階段管理的程式碼。To put it very simply, managed code is just that: code whose execution is managed by a runtime. 在此情況下，不論實作為何 (Mono、.NET Framework 或 .NET Core)，所提到的執行階段都稱為 Common Language Runtime 或 CLR。In this case, the runtime in question is called the Common Language Runtime or CLR, regardless of the implementation (Mono or .NET Framework or .NET Core). CLR 負責將 Managed 程式碼編譯成機器碼，再加以執行。CLR is in charge of taking the managed code, compiling it into machine code and then executing it. 此外，執行階段提供幾項重要服務，例如自動記憶體管理、安全性界限、型別安全等。On top of that, runtime provides several important services such as automatic memory management, security boundaries, type safety etc.
與此相反的是您執行 C/C++ 程式的方式，又稱為「Unmanaged 程式碼」。Contrast this to the way you would run a C/C++ program, also called "unmanaged code". 在 Unmanaged 世界中，程式設計人員會負責處理幾乎所有工作。In the unmanaged world, the programmer is in charge of pretty much everything. 實際程式基本上是作業系統 (OS) 載入記憶體並啟動的二進位檔。The actual program is, essentially, a binary that the operating system (OS) loads into memory and starts. 從記憶體管理到安全性考量等其他工作都是程式設計人員的責任。Everything else, from memory management to security considerations are a burden of the programmer.
Managed 程式碼是以其中一種高階語言撰寫而成 (例如 C#、Visual Basic、F# 等)，並可在 .NET 上執行。Managed code is written in one of the high-level languages that can be run on top of .NET, such as C#, Visual Basic, F# and others. 當您使用這些語言各自的編譯器來編譯以這些語言撰寫的程式碼時，您不會取得機器碼。When you compile code written in those languages with their respective compiler, you don’t get machine code. 您會取得稍後可供執行階段編譯及執行的中繼語言碼。You get Intermediate Language code which the runtime then compiles and executes. 這項規則的唯一例外是 C++，因為它也會產生在 Windows 上執行的原生、Unmanaged 二進位檔。C++ is the one exception to this rule, as it can also produce native, unmanaged binaries that run on Windows.
中繼語言和執行Intermediate Language & execution
什麼是「中繼語言」(簡稱 IL)？What is "Intermediate Language" (or IL for short)? 它是以高階 .NET 語言撰寫之程式碼的編譯結果。It is a product of compilation of code written in high-level .NET languages. 當您編譯以其中一種語言撰寫的程式碼之後，您會取得由 IL 所組成的二進位檔。Once you compile your code written in one of these languages, you will get a binary that is made out of IL. 請務必注意，IL 與在執行階段上執行的任何特定語言都無關；它甚至有個別的規格，如果您想要，可以閱讀此規格。It is important to note that the IL is independent from any specific language that runs on top of the runtime; there is even a separate specification for it that you can read if you’re so inclined.
當您從高階程式碼產生 IL 之後，您可能想要執行它。Once you produce IL from your high-level code, you will most likely want to run it. 此時 CLR 會接管，並開始 Just-In-Time 編譯 (或 JIT-ing) 的程序，以將您的程式碼從 IL 編譯成可實際在 CPU 上執行的機器碼。This is where the CLR takes over and starts the process of Just-In-Time compiling, or JIT-ing your code from IL to machine code that can actually be run on a CPU. 如此一來，CLR 就會知道您程式碼的實際功能，並可有效地進行「管理」。In this way, the CLR knows exactly what your code is doing and can effectively manage it.
中繼語言有時也稱為通用中間語言 (CIL) 或 Microsoft Intermediate Language (MSIL)。Intermediate Language is sometimes also called Common Intermediate Language (CIL) or Microsoft Intermediate Language (MSIL).
Unmanaged 程式碼互通性Unmanaged code interoperability
當然，CLR 允許超過 Managed 與 Unmanaged 世界之間的界限，而且有許多程式碼會這樣做，甚至是在基底類別庫中。Of course, the CLR allows passing the boundaries between managed and unmanaged world, and there is a lot of code that does that, even in the Base Class Libraries. 這稱為互通性 或簡稱 Interop。This is called interoperability or just interop for short. 這些佈建可讓您包裝並呼叫 Unmanaged 程式庫。These provisions would allow you to, for example, wrap up an unmanaged library and call into it. 不過，請務必請注意，一旦您這樣做，當程式碼超過執行階段的界限時，執行的實際管理會再次交由 Unmanaged 程式碼，因此會受到相同的限制。However, it is important to note that once you do this, when the code passes the boundaries of the runtime, the actual management of the execution is again in the hand of unmanaged code, and thus falls under the same restrictions.
同樣地，C# 語言可讓您在程式碼中直接使用 Unmanaged 建構 (例如資料指標)，方法是利用所謂的 unsafe 內容，來指定程式碼片段的執行不受 CLR 管理。Similar to this, C# is one language that allows you to use unmanaged constructs such as pointers directly in code by utilizing what is known as unsafe context which designates a piece of code for which the execution is not managed by the CLR.