Stack Exchange
How does LINQPad compile code?
I am guessing it neither invokes csc.exe or implement an entire compiler, so how does it work?
Update: Thanks to Jon Skeet for the pointer to code that was easy to learn from.
string c = @"
public class A
{
    public static void Main(string[] args)
    {
        System.Console.WriteLine(""hello world"");
    }
}
";

CodeDomProvider compiler = new CSharpCodeProvider();

CompilerParameters parameters = new CompilerParameters();
parameters.WarningLevel = 4;
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;

CompilerResults r = compiler.CompileAssemblyFromSource(parameters, c);

Assembly a = r.CompiledAssembly;

Type[] ts = a.GetTypes();

Type t = ts[0];

object o = t.GetMethod("Main").Invoke(null, new object[] { new string[] { } });


csc.exe, it's what makes System.CodeDom work.  And the IDE.

@Hans: Well, almost. It would be more accurate to say that both csc.exe and the IDE share a common library that performs code analysis.

+1 This is a great question and Jon Skeet does it AGAIN!

From "How LINQPad Works":

LINQPad compiles your queries using .NET's CSharpCodeProvider (or VBCodeProvider)

Obviously there's rather more to it, but that's the bit you asked about - read the link for more details.
If you want to have a look at a rather more simplistic implementation, you could download the source code for Snippy, the little tool I created for C# in Depth. Again, it uses CSharpCodeProvider - and it's a simple enough example that it's easy to understand, with any luck. (There are only a few classes involved, IIRC.)

@AaronAnodide Oh my goodness... IT ALL MAKES SENSE NOW! Mr. Skeet, you have enlightened many SO-ers with your wisdom and we are eternally grateful (if I didn't thank him, I'd probably disappear tonight).

Jon's answer from almost 5 years ago is now out of date.
From "How LINQPad Works" (as at 29 Jan 2016):

LINQPad 5 compiles your queries using the Microsoft Roslyn libraries
  (in the past it used .NET's CSharpCodeProvider and VBCodeProvider).

You can see an example of how to use Roslyn to compile your code here: 
Learn Roslyn Now - Part 16 - The Emit API

Jon's answer was correct when he wrote it. But then LINQPad 5 came along. It's amazing!
