You don’t need Visual Studio for simple C# projects. Everything you need is included when you install the .NET Framework, except a decent text editor. (I highly recommend Notepad++) To get started, you’ll need to install the .NET Framework, if you haven’t already done so, and set up several environment variables. I first create a user environment variable named DOT_NET_HOME whose value is the absolute path to the location of the csc.exe file for your particular version of .NET. (See Figure 1)
I then edit the user’s Path environment variable to include the DOT_NET_HOME variable. To reference one environment variable in another, enclose the referenced variable in percent characters like so: %DOT_NET_HOME% For a detailed tutorial on how to set environment variables in Windows 10 you can watch this video:
Next, create source files with your text editor of choice. For this example I’ll create a class named MyClass with Notepad++ as shown in Figure 2.
Save the file and enter the source code for your class. I’ll keep this example simple. Here’s the code:
using System; public class MyClass { public static void Main(){ Console.WriteLine("Hello World!"); } }
Here’s my finished source file:
Open up a console window and navigate to where you saved the source file and use the csc command to compile the file like so:
csc MyClass.cs
If it compiles successfully the result will be a new file named MyClass.exe. Here are the results of compiling and running MyClass:
That’s it!
Parting Thoughts
You can compile C# source files from the command line using the C# compiler that ships with the .NET Framework. If you know how to do this you’ll have a better understanding of what goes on under the covers in Visual Studio. You can also use the C# compiler with other source code project management utilities like GNU make, Gradle, Apache Maven, etc.
Rick Miller
Falls Church, Virginia