Get up to 80 % extra points for free! More info:

Lesson 2 - Visual Studio and your first C# .NET console application

Lesson highlights

Are you looking for a quick reference on creating a C# project in Visual Studio instead of a thorough-full lesson? Here it is:

  • Download Visual Studio Community Edition at https://www.visualstudio.com/…nity-vs.aspx
  • Install Visual Studio and check to install .NET desktop development kit, choose C# if needed
  • Run Visual Studio and select File -> New -> Project in the application menu:
Creating a new project in Visual Studio - C# .NET Basic Constructs
  • Select the Visual C# template -> Windows and in the next menu select Console Application:
Creating a new console application in C# .NET - C# .NET Basic Constructs
  • Edit the project name and location if needed and confirm the form
  • Hit the green play button in the toolbar to run the project.

Would you like to learn more? A complete lesson on this topic follows.

In the previous lesson, Introduction to C# and the .NET framework, we talked about the language itself and went over what the .NET framework is. In today's lesson, we're going to focus on the Visual Studio IDE. We'll show you how to use it and program a simple console application.

IDE stands for Integrated Development Environment. In a nutshell, it's an application in which we'll write a source code, and then use it to run, test and debug our application.

Of course, we'll have to start by installing Visual Studio. If you have access to paid versions, which come with advanced features, go ahead and use it, but the free Community version will be more than enough for us. You can download the Visual Studio 2015 Community Edition on the official Microsoft site.

Installation

Even if English isn't your native language, I highly suggest installing the English version of Visual Studio. The advantage to using English applications is that if you run into problems, you will most likely not be able to find the answers in your language (e.g. with the advanced configuration of databases or websites). All of your applications and programs should be also written in English, but for the start feel free to use your native language without using accent characters, if your language has any. If you want to become a professional programmer, you should try to improve your language skills, and write everything in English.

The installation will ask about the preferred language, choose C# and continue by clicking the next button :). After installation, you may need to register your Visual Studio. Registration is free, after which you'll receive a serial number that allows you to use this program legally.

Backup and version control

Programmers usually need a tool that will provide version control and backup of his work. We can't rely on the fact that we could just save the program because we're humans and humans make mistakes. When you lose a few days' or even a few weeks' work, it can be really demotivating. It's good to think about such situations right from the start. I highly recommend Dropbox, which is extremely simple, and automatically stores multiple versions of your files, which makes it possible to revert to previous versions of the project, and also synchronizes with a web repository. Even if you've accidentally deleted your project, overwrote it, somebody stole your laptop or your hard drive accidentally collapsed, your data will remain safe. Dropbox also allows you to share projects with several developers.

You can also use a tool called GIT for the same purposes, but its configuration would require the "while" article. Dropbox is perfect for our current intents and purposes.

Creating a project

Run VS and select File -> New -> Project in the application menu.

Creating a new project in Visual Studio - C# .NET Basic Constructs

In the New Project window, select the Visual C# template -> Windows and in the next menu select Console Application. Let's name this project FirstApplication. Then, switch the target framework to the .NET Framework 3.5 if you want to support Windows 7 users without additional framework installation (for Windows 8 and above choose .NET 4.5). Create a folder for your projects in your Dropbox folder, for example, csharp. Use the Browse button and select a folder C:\Users\your_name\Dropbox\csharp. We will stick with console applications, command line interface, for a while because it needs minimal knowledge of the object-oriented world, and they are ideal for learning the basics of the language. The window should look like this:

Creating a new console application in C# .NET - C# .NET Basic Constructs

Confirm the form.

Visual Studio Tutorial

The window now looks like this (I have resized it to fit :) ):

Visual Studio window - C# .NET Basic Constructs

We're interested mainly in the middle window in which VS will generate some source code. It may be a surprise to some of you that we are not starting out with an empty window, but then again that is exactly what a template is (easily modifiable preset code)! We'll keep it simple for now, everything will be explained throughout the courses. Some parts are quite advanced, so we'll just accept the fact they're there and go into detail later.

The first few lines are telling us what libraries from .NET we are using. The most important one for us is System. Without it, we could hardly program anything, because it contains basic methods for working with the console. We won't pay any attention to namespace and class, just keep in mind that C# uses them to structure programs. The key will be the Main() method for us, we'll write our code between the curly brackets below it, i.e. the body. Main is a reserved word, and C# knows that this method must be executed as first after the application starts (there are more methods, but more on that later). Actually, we'll ignore everything except for the Main() method.

The second important element in the window is a green "Play" button in the upper bar, which compiles and runs our program. You can try it, but our program will not do anything, just turn terminate right away. You can also run the program with the F5 keyboard shortcut. Shortcuts in VS are designed very well and it will simplify your work a lot, that is if you decide to learn them.

Near the arrow icon, we have Debug selected. This means that the program will be compiled in the Debug mode, and it'll contain certain routines to debug errors. It's mainly used to test the program when we're developing it, and the program may run slower due to it. Once you're sure that your program is complete, switch it to Release mode and run the program. As a result, we'll have a program which can be distributed among people.

Directory structure of the console application

Let's take a look at what's in our application folder. Open it up, mine's in C:\Users\your_name\Dropbox\csharp\FirstApplication\, and find a file named FirstApplication.sln. Sln stands for " Visual Studio solution ". It's a set of projects and it may contain multiple applications which are used for multi-tier applications, or for testing. We'll open our applications using this file. There should also be a folder named FirstApplication\ containing our project. Let's open it up, and see what's inside.

The FirstApplication.csproj file contains a project file that we can use to open up our app. Program.cs contains the actual source code. We are also interested in the bin/ folder, whose name suggests that it contains binary (machine) code of our application.

Inside the bin folder, you'll see that it contains Debug/ and Release/ subfolders. There are .exe files of our application inside of it (that is if we run the application at least once in the corresponding mode). In order to show your application to somebody, this .exe file in the Release/ subfolder is the file you would have to send him or her. Don't worry about any of the other files, for now.

Hello world

As tradition instructs, we'll make the first program most people make when they learn a new language -Hello World. This is a program that displays "Hello world" or some similar text. Again, let me remind you that we'll write commands in the body of the Main() method. We'll need two commands (note: I'm using the word command just for simplification): first one to display the text and the second one to wait for the user to press any key to prevent the program from terminating immediately.

To write text we use the following bit of code:

Console.WriteLine("Text");

Then, we wait for the user to press a key:

Console.ReadKey();

Console is a Class. For now, we'll see classes as sets of available commands. In C#, commands are called methods. So console includes methods for console operations. We call the WriteLine() method from it that displays a text. You can see that we use a dot operator (.) to call the method in the class. Methods can receive input parameters, which are entered in parentheses and separated by commas. In the case of the WriteLine() method, the parameter is the text to be printed. In programming, texts are called strings, like a string of characters, and written it in quotation marks. C# sees the quotation marks and interprets it as text. The ReadKey() method has no parameters, but we still have to add parentheses after the method name because it is C# needs it to be this way. We should write the commands on separate lines, and after every line, we should put a semicolon (;). Our Main() method will now look like this:

static void Main(string[] args)
{
    Console.WriteLine("Hello ICT.social!");
    Console.ReadKey();
}

You can run the program by pressing F5 if your system hasn't assigned a different command for the key. You can also run code snippets from our articles by pressing the "Play" button above, you can edit the code as well, just click on it. Try that :) This makes you able to study from anywhere, even without your IDE.

Your first C# .NET app
Hello ICT.social!

Congratulations, you have just become a programmer :) That will be all for today, in the next lesson, Variables, type system and parsing in C# .NET, we'll look at the basic data types and create a simple calculator.

Today's project is attached as a file at the end of the article. You could always download the result below each lesson. I suggest that you create a project using the tutorial, and if something in your project doesn't work, then download it to find a mistake. If you download the lesson code before even trying to make it yourself, you won't learn anything :)


 

Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to the license terms

Downloaded 94x (32.6 kB)
Application includes source codes in language C#

 

Previous article
Introduction to C# and the .NET framework
All articles in this section
C# .NET Basic Constructs
Skip article
(not recommended)
Variables, type system and parsing in C# .NET
Article has been written for you by David Capka Hartinger
Avatar
User rating:
35 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities