Visual Studio is a world class IDE that is used by thousands and thousands of people worldwide to create a variety of solutions. No other IDE comes close. There is even a free Community Edition that is great for people starting out learning C#.
Note: This post features in the Awesome Third Annual C# Advent Calendar (23rd December).
Visual Studio is Awesome
In fact, I love Visual Studio so much that I created a website VisualStudioTips.co.uk that describes over 30 tips for Visual Studio, which if you use Visual Studio a lot, I seriously recommend you check out. This December it is currently appearing as an advent calendar but will revert back to normal after Christmas. End plug.
Coding without Visual Studio
However, as amazing an IDE it is, did you know that you don't technically need it to compile your C# code? It's true.
I'll repeat that, if you do not have Visual Studio installed, but you do have the .NET Framework or Windows SDK installed*, you have a compiler that is capable of compiling your c# code.
Don't get me wrong, if you have the choice of writing and compiling your code in Visual Studio or not, you're going to always choose Visual Studio right? So would I.
However, many years ago, when I first starting to learn C# using the early versions of the .NET Framework, I thought it was interesting to understand what Visual Studio was doing under the hood. So I made sure I knew how to compile C# code without using Visual Studio and use the compiler directly itself.
So how does one do that I hear you say? Good question.
Writing the very simple code
First, let's write a very simple executable that just outputs some 10 random numbers between 0 and 100 to the console. If you didn't already know, you need to provide a static method called Main in an executable, this is the default entry point into a console application.
Compiling the very simple code
As far as I am aware, if you have the .NET Framework installed, the latest version of the csharp compiler lives at C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
All you need to do to compile your code is to call that compiler exe and provide a couple of parameters.
- The name of the .exe or .dll you want the compiler to create (the /out parameter)
- The .cs files that contains your code
So if you run the following command..
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /out:"c:\development\simplecompile\SimpleProgram.exe" c:\Development\SimpleCompile\SimpleProgram.cs
You will see an output like this..
This has actually successfully compiled the code and produced the executable at the location you specified (c:\development\simplecompile\SimpleProgram.exe).
Now I've not done this in a while so interestingly, these days it would seem the compiler warns that language features after C# 5 aren't supported using this method. This can be confirmed and demonstrated by trying to use string interpolation for example. Seeing this message, i'm sure there is a more up-to-date way of doing this to get access to the latest language features. There is I believe a more up-to-date version of the compiler which ships with Visual Studio, but this blog post is all about compiling without Visual Studio so can live with the C#5 language limitation for the purpose of this post.
Running the very simple code
Anyway, you can go ahead and run the program.. and see the result..
There you go, a fully working .NET executable written in C# without an IDE.
There are of course other things you can do which I have not shown. For example, take and compile multiple .cs files into the output or output a .dll instead of an exe.
Why might you not have Visual Studio again?
So why exactly, might you not want/be able to use Visual Studio to compile your code?
- Perhaps the code is being written, compiled and run on a very low spec machine in a developing country?
- Perhaps there is no room, time or connectivity to install Visual Studio?
- Perhaps you want to learn to code without an IDE?
- Perhaps you are a technological masochist?
So the next time you find yourself stranded on a dessert island with only the Microsoft.NET Framework installed on your laptop with no Visual Studio in sight.. don't worry, you'll still be able to write and compile C# code..
Though if you're stranded on a dessert island, you'll probably have other problems to deal with. But not being able to compile C# code?.. wont be one of them.
* I believe you can also get a c# compiler if you install the Windows SDK, MSBuild or specific compiler nuget packages