© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
R. VystavělC# Programming for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-7147-6_2

2. Your First Program

Radek Vystavěl1  
(1)
Ondřejov, Czech Republic
 

You have your computer ready now, so let’s start programming! In this chapter, you will create your first program in the C# language and learn all the steps that you need to perform to do this.

Seeing It in Action

In this chapter, you’ll create a program that displays message “I am starting to program in C#.” to the user (see Figure 2-1).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig1_HTML.jpg
Figure 2-1

Your first program

Creating the Project

You start every new program by creating a new project, so let’s do that now.

Launching Visual Studio

Launch Visual Studio. Start screen similar to Figure 2-2 should appear.
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig2_HTML.jpg
Figure 2-2

Visual Studio Start screen

Drag the scrollbar down and choose the “Create a new project” action (see Figure 2-3).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig3_HTML.jpg
Figure 2-3

Choosing for a new project

../images/458464_2_En_2_Chapter/458464_2_En_2_Fig4_HTML.jpg
Figure 2-4

Selecting a project template

Creating New Project

The dialog that appears (see Figure 2-4) requires you to choose a new project template. Select the Console App C# template and press the Next button.

In the next dialog (see Figure 2-5), type “My first program” as a new project name and press the Create button.
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig5_HTML.jpg
Figure 2-5

Entering a name for your new project

Writing the Program Code

The most important step is writing the program’s code, so read on.

The Look of the Development Environment

After project creation, Visual Studio looks like Figure 2-6.
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig6_HTML.jpg
Figure 2-6

The look of Visual Studio

The main part of the development environment window is occupied by the source code editor. In it, the Program.cs file is opened, as is suggested by the tab’s title. Program.cs is the main file of your new Console project. As you can see, it already contains some source code.

You might wonder where this code came from. You haven’t written any line of code yet! The answer is that Visual Studio generated the code when you selected the Console App template. As you saw when creating the project, Visual Studio contains many different templates; these templates are ready-made project skeletons for different types of programs.

You can see that the code contains some strange words like using, namespace, class, and so on. I am not going to explain these now because you do not need a detailed understanding of them at this time. But Visual Studio needs these lines, so just leave them alone. What you do need to know is where to write your own statements, which is what I will explain next.

Knowing Where to Write Statements

You write program statements between the curly brackets that you find after the line containing the word Main (see Figure 2-7).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig7_HTML.jpg
Figure 2-7

Where you write your statements

In all previous Visual Studio versions, the space between curly brackets was left blank. Now the IDE creators decided to include a single line of code which you may delete or modify subsequently.

Writing the Code

In this case, type the following statements between the curly brackets after the Main line. Make sure to type them exactly as you see here. Differences between lowercase and uppercase matter, and semicolons matter, too!
// Output of text to the user
Console.WriteLine("I am starting to program in C#.");
// Waiting for Enter
Console.ReadLine();
Visual Studio now looks like Figure 2-8.
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig8_HTML.jpg
Figure 2-8

Entering your first code statements

Please double-check that you typed the statements in the same place as I did. Again, they have to be between the brackets. Also, be careful of the brackets. Do not accidentally delete any of them.

The source code of Program.cs now looks like this:
using System;
namespace My_first_program
{
    class Program
    {
        static void Main(string[] args)
        {
            // Output of text to the user
            Console.WriteLine("I am starting to program in C#.");
            // Waiting for Enter
            Console.ReadLine();
        }
    }
}

Understanding Your First Statements

What do these statements do?
  • Console.WriteLine outputs (writes) a single line to the user.

  • Console.ReadLine, in general, reads a line of text that the user enters with the keyboard. In this case, however, the purpose of the statement is to make your program wait for the user to press Enter when everything is done. In other words, you do not want the program window to disappear immediately.

  • Everything following the two slashes (//) until the end of a corresponding line is ignored. This text contains your remarks/comments. Visual Studio colors them in green.

Using IntelliSense

You probably have noticed that when you type, Visual Studio offers you available possibilities (see Figure 2-9). You can choose an option either using the mouse or using the arrow keys followed by pressing the Tab key.
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig9_HTML.jpg
Figure 2-9

Using IntelliSense

The part of Visual Studio that provides you with these hints is called IntelliSense. Get used to relying on it as much as you can. It is the best way to avoid unnecessary typos.

Saving the Project

You have written several lines of code, so you probably want to save them. According to the default settings of Visual Studio, projects are automatically saved before every program launch. However, sometimes you want to save the changes manually. In that case, choose File ➤ Save All from the Visual Studio menu, or click Ctrl+S.

Launching Your Program

Having written your program, you usually want to launch it to see it “in action” and to check whether it does what you meant it to do.

Prepare yourself. The great moment of your first program launch is coming! Choose Debug ➤ Start Debugging from the Visual Studio menu, or just press the F5 key.

Visual Studio builds and launches your program (see Figure 2-10). The program outputs the specified text and waits for the Enter key to be pressed, which is exactly the way you have programmed it.
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig10_HTML.jpg
Figure 2-10

Launching your program

Now in the role of the user, press the Enter key. The program terminates, and the “black window” disappears. Actually, it was the behavior of Visual Studio up until now. In the present version, there is a new measure added preventing the console window from disappearing as you can see after hitting the Enter key (Figure 2-11).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig11_HTML.jpg
Figure 2-11

The added measure preventing the window from disappearing

Since Console.ReadLine() prevents the window from disappearing also when running outside of Visual Studio, I will be including this statement in all our programs. If you want, you can disable the new Visual Studio measure according to the instructions given in the message of Figure 2-11.

In Visual Studio, select the Tools ➤ Options menu. In the dialog, click the Debugging group, then tick the option “Automatically close the console when debugging stops,” and confirm by pressing OK (see Figure 2-12).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig12_HTML.jpg
Figure 2-12

Disabling the new measure

Note

With the default settings of your computer, your programs will appear with white type on a black background, as you can see in Figure 2-10. However, for better readability, I will show all the later screenshots in black type on a light background (see Figure 2-11).

Changing Text Size

Do you think the outputted text is too small? Do you need to enlarge the font your programs will use?

If so, click the title bar icon at the upper-left corner of the “black screen” of the launched program and choose Defaults (see Figure 2-13).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig13_HTML.jpg
Figure 2-13

Choosing Default settings

Then click the Font tab, change the font according to your preferences, and confirm the change by clicking the OK button (see Figure 2-14).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig14_HTML.jpg
Figure 2-14

Changing the font

When the next program launches, the new font will be used.

Dealing with Errors

If you did not write the statements exactly the way I showed you or you wrote them in the wrong place, the program build would terminate unsuccessfully with errors.

Let’s try this! Delete the semicolon at the end of the line with the Console.WriteLine statement.

When you try to launch your program (by pressing the F5 key), the trial terminates with an error dialog (see Figure 2-15).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig15_HTML.jpg
Figure 2-15

Getting an error

In this dialog, always click No; you do not want to run some older version of your program (if it exists).

After clicking No, the Error List pane appears (see Figure 2-16) at the bottom.
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig16_HTML.jpg
Figure 2-16

Error list

Return the deleted semicolon, and everything will be fine again. In the future, it may be more difficult to find what you did wrong, especially at the beginning of your programming career. That’s OK—my opinion is that you can’t become an expert in a field until you have made all the possible mistakes there are.

Finishing Your Work

You have just gone through all the essential steps of program development. You will proceed along the same lines in every future project you do.

You now need to learn how to terminate your work and how to get back to it later. The former is simple; you can finish your work on this project by choosing File ➤ Close Solution from the menu or by closing the whole Visual Studio program.

Restoring Your Work

When you want to get back to your project later, you can reopen it in Visual Studio using one of the following ways:
  • From the Start Page: This is the page that appears immediately after Visual Studio starts and contains links to your recent projects (see Figure 2-17). Simply click the right one.

../images/458464_2_En_2_Chapter/458464_2_En_2_Fig17_HTML.jpg
Figure 2-17

Using the Start Page

  • From the Open Project dialog: Select File ➤ Open ➤ Project/Solution from the Visual Studio menu. The Open Project dialog appears in which you should locate your project (see Figure 2-18). Specifically, look for files with the .sln extension. If you cannot see the file extensions, turn their display on in Windows File Explorer (on the View tab, select the “File name extensions” check box), as shown in Figure 2-19.

../images/458464_2_En_2_Chapter/458464_2_En_2_Fig18_HTML.jpg
Figure 2-18

Opening your program with the Open Project dialog

../images/458464_2_En_2_Chapter/458464_2_En_2_Fig19_HTML.jpg
Figure 2-19

Showing extensions

  • From the Recent Projects menu: Select File ➤ Recent Projects and Solutions. Visual Studio remembers what project you were working on recently. Just choose the appropriate project (see Figure 2-20).

../images/458464_2_En_2_Chapter/458464_2_En_2_Fig20_HTML.jpg
Figure 2-20

Opening your project from the File menu

Transferring Your Work

You may also be interested in how to transfer your project to somewhere else from your computer, so you can work on it later on a different computer. The answer is simple. If you’re using a flash drive, OneDrive, or something similar, just transfer the project’s whole folder.

Using Solution Explorer

There is an important issue with transferring your projects onto another computer that you should be familiar with. Sometimes, a project is opened without a source code editor (see Figure 2-21).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig21_HTML.jpg
Figure 2-21

Opening a project without a source code editor

What do you do in such a situation? There is a pane (subwindow) called Solution Explorer usually located at the right side of the Visual Studio window. Simply double-click your source code file, Program.cs (see Figure 2-22).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig22_HTML.jpg
Figure 2-22

Opening your source code via Solution Explorer

Did even Solution Explorer disappear? No problem! At any time, you can display it using the menu selection View ➤ Solution Explorer (see Figure 2-23).
../images/458464_2_En_2_Chapter/458464_2_En_2_Fig23_HTML.jpg
Figure 2-23

Opening Solution Explorer

Summary

In this chapter, you made your first program, and you also started learning about how to work with the Visual Studio development environment. You went through all the steps of program development, which essentially are these:
  • Creating the project

  • Editing the source code

  • Saving the source code

  • Launching the program

  • Detecting and removing possible errors

  • Transferring your project onto another computer of yours to work on it elsewhere

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.40.177