Lesson 2 - Visual Studio and your first console application

C# .NET Basic constructs Visual Studio and your first console application

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

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_na­me\Dropbox\cshar­p. 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

Confirm the form.

Visual Studio Tutorial

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

Visual Studio window

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_na­me\Dropbox\cshar­p\FirstApplica­tion, and find a file named FirstApplicati­on.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 FirstApplicati­on.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, 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 :)


 

Download

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

 

 

Article has been written for you by David Capka
Avatar
Do you like this article?
3 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 College The author learned IT at the Unicorn College - a prestigious college providing education on IT and economics.
Activities (21)

 

 

Comments

Avatar
Flavio Dorobosi Gi Mata:11/23/2016 2:50

no i am no have any source code.thank you.

 
Reply 11/23/2016 2:50
Avatar
David Capka
ICT.social team
Avatar
Replies to Flavio Dorobosi Gi Mata
David Capka:11/23/2016 3:27

If you're saying you can't see the source code, open the Program.cs file in the Solution Explorer on the right, maybe the tab didn't open by itself.

Edited 11/23/2016 4:06
Reply 11/23/2016 3:27
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
Adebayo Habeebullahi:21. January 17:25

wow so cool to be a programmer

 
Reply  +1 21. January 17:25
Avatar
woodlet noel
Member
Avatar
woodlet noel:1. June 7:43

I'm so excited to see my dream is loading, to become a real programmer!

 
Reply 1. June 7:43
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

4 messages from 4 displayed.