C# applications require Microsoft .NET Framework to run. If you have Microsoft .NET framework installed on your system, you can simply write C# applications in any text editor and compile them via command-line tools. However, this is not the way I recommend to beginner C# programmers. Rather, I recommend that you use an IDE (Integrated Development Environment). An IDE offers lots of benefits such as syntax highlighting, easy compilation, modularization and documentation of an application.
For the sake of this course, I will be using the Visual Studio Code. In this lesson, you will see how to install the Visual Studio Code and how to run your first application in C#.
Follow these steps to set up the environment required to run the C# applications.
- To download Visual studio code, go to this link: https://code.visualstudio.com. Click on “Download for Windows”. An executable file will be downloaded to your system. Open the executable file. Accept the license agreement and click the “Next” button. You should see the following window:
- Click on the “Next” button on the above screen. You will then see a window named “Ready to Install”. Click the “Install” button on that window as shown below:
- The installation process will start. Once the installation step completes, you should see the following window:
Running Your first Program
- Before you can run C# programs in VS code, you need to install C# extension. In the visual studio code, select “View” from the top menu and then click “Extension”. You can open the extension search bar by typing “Ctrl + Shift + X”.
- Enter “C#” in the search bar for extensions, and select the first option as shown below:
- Once the C# extension is installed, from the menu select “New->Open Folder”. You will see the “Select Folder” dialogue box. Here create your new folder. I named the folder, “C# Project”.
- Next, from the top menu of the visual studio code select “Terminal -> New Terminal”. In the terminal type “dotnet new console” as shown below:
- In the file window on the left, you will see a file “Program.cs” file under “C# Project” folder. Open this file. By default, it contains some code as shown below:
- Now there are two ways to run this code. The first method is via terminal. In the terminal type the following command:
The above command will build the code. Next run:
The above command will run your code and you will see “Hello World!” printed in the output as shown below:
- The other way to run the code is by simply pressing “F5” or by clicking “Debug – Start Debugging” from the top menu of the visual studio code.
Before the end of this lesson let me briefly explain what is happening in the code. The code starts with “using System” which means that we are using the “System” library which contains system-related utilities. Next we have “namespace C_Project”. Since C# is object-oriented programming, therefore everything happens inside a class (we will see classes in detail in a later lesson). A class exists inside a namespace. Next, we have the class “Program”. Inside the class, we have one method “Main”. The static “Main” method is a starting point of a C# application. Inside the “Main” method we use “Console.WriteLine” method that writes a message “Hello World” on the console.
In this lesson, you saw how to set up the environment required to run C# applications. In the next lesson, you will start exploring fundamental C# concepts starting data types and variables in C#.