What exactly is "managed" code?


Question

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

1
53
5/21/2016 8:35:41 AM

Accepted Answer

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.

36
9/13/2016 8:56:08 PM

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon