Compile C#: How To Compile C# Code Without Using Visual Studio

C# prgrammers know that you can compile C# Code using Visual Studio. However, there also is a way to compile C# without having VS installed.

If you are familiar with C#, you probably know that programmers usually use Visual Studio for writing and compiling C# code. However, there are many situations in which you may want to compile C# code on a computer that hasn’t got Visual Studio installed. Because C# uses the .NET Framework by Microsoft, almost any Windows computer can compile C# code by default.

Keep reading to find out how to compile C# without having to use Visual Studio.

Add The C# Compiler To The Path

To get started, we are going to add the C# compiler to the path so that we can use it from the console without having to write out the entire path to the compiler.

If you don’t want to add the compiler to the path, you can skip to the next step, but keep in mind that you will have to enter the entire path to the compiler when using it.

First, you need to open the environment variable settings by searching for “edit environment variables” in the windows search.

In the window that just opened, click on “Environment Variables…”

Compile C# - Environment Variables

Next, search for “Path” under System Variables and double-click it.

Edit Path

Now, just add a new entry with this path: C:\Windows\Microsoft.NET\Framework\v4.0.30319

Your version may vary from the given one. If so, simply search for the latest version under the Framework directory.

Compile C# - Add Path Entry

Finally, hit OK on every window to close them and save the changes.
If you already have a CMD/PowerShell window open, you may need to close and open it again.

Compile C# Code

To now compile a C# source file to an exe file, there’s nothing left to do besides opening a CMD or PowerShell window and entering this command:

csc.exe <source file>

If you didn’t add the compiler to the path, you need to specify the entire path to the compiler:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe <source file>

Creating a library (dll file)

If you want to create a library instead of an exe file, you can simply add this argument before the source file specification:

/target:library

For example:
csc.exe /target:library test.cs

Thanks for reading!

Share this article

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *