When developing applications for the .NET Framework, you’ll most likely use an IDE like Visual Studio, Visual Studio Express, SharpDevelop or MonoDevelop. These tools make it easy to write code and really boost productivity. But you should know that you could write code without any of these tools.
First, let’s download the latest version of .NET from Microsoft’s website if you don’t have it yet. Latest version is Microsoft .NET 4. The installation is simple.
When you install the .NET Framework runtime, the Software Development Kit (SDK) comes included. If you look in the directory C:\WINDOWS\Microsoft.NET\Framework\v<version number>, you’ll find the following programs:
- csc.exe – C# compiler
- vbc.exe – VB.NET compiler
- aspnet_compiler.exe – ASP.NET compilation tool
A compiler and your favorite text editor is all you need to start writing apps for the .NET Framework. With several of the IDE’s I mentioned before being free, there’s really no reason for you to write code “by hand” in a text editor. Still, it’s a good exercise to understand what your IDE is doing in the backend.
Write Your First Program
We’re going to write a little hello program and compile it with the .NET SDK. Open you favorite text editor and type the following code. We’ll use C# for this sample.
// Include the System namespace using System; // This class will contain the entry point of the program class MyFirstApp { // This is the entry point of the program public static void Main() { // Write something Console.WriteLine("Hello World!"); } }
That’s it. Save the file somewhere in your computer as MyFirstApp.cs. I saved mine to C:\Blog\Code\NET\MyFirstApp\MyFirstApp.cs.
Breaking Down the Code
We start the program importing the classes that we’ll use in the program. The using keyword specifies the namespace containing the classes that we wish to import. We can import as many namespaces as we need. We’ll discuss namespaces in another post.
Next we define a class. C# is object oriented, and classes are the core of object-oriented programming. All code in C# needs to be contained in methods, and methods need to belong to a class.
Now we define the entry point of the program. The compiler expects a method called Main and marked with public and static.
Next line just writes a string to the console. We invoke the WriteLine method from the Console class.
Compile and Run the Program
So we’re ready to compile our code into an executable file for Windows. Open a command prompt (Start –> All Programs–> Accessories –> Command Prompt) and navigate to C:\Windows\Microsoft.NET\Framework\v<version number> folder. I’ll use .NET 4 here.
Now we just need to call the compiler and feed it with the target file we want to create (MyFirstApp.exe) and the file with out source code. The compiler doesn’t give any feedback if the operation was successful, other than the copyright information of the compiler. If there was a problem, we’ll receive information about the error.
Now all we need to do is navigate to the target path and execute our little app. You can also open the folder and double-click on the file.
And there it is, our message on the console. Please leave a comment if you had any issue or if you have any question.