Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is "managed" code?

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 managed code mean?

Wikipedia describes it simply as

Code that executes under the management of a virtual machine

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

  • 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)?

In a similar vein,

  • is .NET a language or a framework, and what exactly does "framework" mean here?

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...

like image 305
Graeme Perrow Avatar asked Sep 11 '08 23:09

Graeme Perrow


People also ask

What is meant by managed code?

To put it very simply, managed code is just that: code whose execution is managed by a runtime. In this case, the runtime in question is called the Common Language Runtime or CLR, regardless of the implementation (for example, Mono, . NET Framework, or . NET Core/.

What is managed and managed codes?

Managed code is the one that is executed by the CLR of the . NET framework while unmanaged or unsafe code is executed by the operating system. The managed code provides security to the code while undamaged code creates security threats.

What is managed code and native code?

Native code is written in the "native" machine language of the computer that it is running on and is executed directly by the processor. Managed code is written in a special language that requires another program to run (i.e. manage) it.

What is managed code and unmanaged code with example?

It gets the managed code and compiles it into machine code. After that, the code is executed. The runtime here i.e. CLR provides automatic memory management, type safety, etc. C/C++ code, called "unmanaged code” do not have that privilege.


2 Answers

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.

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.

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.

like image 198
lnediger Avatar answered Oct 09 '22 22:10

lnediger


I don't think you are alone in being confused about what .Net is. There are already other answers that should have you covered but I'll throw out this tidbit of info for others.

To see what .Net "really" is simply go to c:\Windows\Microsoft.Net\Framework

In there you'll see folders that are specfic to the version(s) you have installed. Go into the v2.0.xxxxx folder if you have it installed for example.

In that folder is the framework. You will basically see a bunch of .exe files and .dll files. All the DLL files that start with System.*.dll is essentially the .Net framework.

The .exe files you'll see in that folder are utilities for developers as well as compilers. You mentioned C#. Find the csc.exe file. That's your C# compiler.

Building a program is really simple. Throw the following code into a hello.cs file.

using System; class Program     {         static void Main(string[] args)         {             Console.WriteLine("hello world");         }     } 

Then on the command line type> csc hello.cs

That will generate you a .exe file. Run it and it will spit out 'hello world' obviously.

The line that says Console.WriteLine() is calling into the Framework. Console is an object that lives within the System namespace and WriteLine() is a static method.

This is the disassembled code for that Console.WriteLine() method:

[HostProtection(SecurityAction.LinkDemand, UI=true)] public static void WriteLine(string value) {     Out.WriteLine(value); } 

When people say things like, "Should I use PHP or .Net?", or "Should I use Python or .Net" you start to see how that's the wrong thing to be discussing. They are obviously comparing a language to a Framework. C# is a language and it is just one of the many languages that can be used to write code on top of the .Net platform. That same method of Console.WriteLine() can be invoked from C#, VB.Net, Pascal, C++, Ruby, Python, F# and any other language that has been made to work on top of the .Net platform.

I hope that helps.

-Keith

like image 42
Keith Elder Avatar answered Oct 09 '22 22:10

Keith Elder