Creating a New C# Project
Before doing anything else in Visual Studio, we must create a new project or load an existing one. The project groups many files, designed to implement a software application or system, in a logical manner. It is recommended that we create a separate project for each new program.
We can create a project in Visual Studio by following these steps:
- File -> New Project …
- The “New Project” dialog appears and lists all the different types of projects we can create. We can choose a project type (e.g. Console Application or WPF Application), programming language (e.g. C# or VB.NET) and .NET Framework version (e.g. .NET Framework 4.5) and give a name to our project (in our case “IntroToCSharp”):
- We choose Console Application. Console applications are programs, which use the console as a default input and output. Data is entered with the keyboard and when a result needs to be printed it appears on the console (as text on the screen in the program window). Aside from console applications, we can create applications with a graphical user interface (e.g. Windows Forms or WPF), Web applications, web services, mobile applications, Windows Store apps, database projects and others.
- In the field "Name" we enter the name of the project. In our case we choose the name IntroToCSharp.
- We press the [OK] button.
The newly created project is now shown in the Solution Explorer. Also, our first file, containing the program code, is automatically added. It is named Program.cs. It is very important to give meaningful names to our files, classes, methods and other elements of the program, so that we can easily find them and navigate the code. A meaningful name means a name that answers the question “what is the intent of this file / class / method / variable?” and helps developers to understand how the code works. Don’t use Problem3 for a name, even if you are solving the problem 3 from the exercises. Name your project / class by its purpose. If your project is well named, after few months or a year you will be able to explain what it is intended to do without opening it and looking inside. Problem3 says nothing about what this project actually does.
In order to rename the Program.cs file, we right click on it in the Solution Explorer and select "Rename". We can name the main file of our C# program HelloCSharp.cs. Renaming a file can also be done with the [F2] key when the file is selected in the Solution Explorer:
A dialog window appears asking us if we want to rename class name as well as the file name. We select "Yes".
After we complete all these steps we have our first console application named IntroToCSharp and containing a single class HelloCSharp (stored in the file HelloCSharp.cs):
All we have to do is add code to the Main() method. By default, the HelloCSharp.cs code should be loaded and ready for editing. If it is not, we double click on the HelloCSharp.cs file in the Solution Explorer to load it. We enter the following source code:
Compiling the Source Code
The compiling process in Visual Studio includes several steps:
- Syntax error check;
- A check for other errors, like missing libraries;
- Converting the C# code into an executable file (a .NET assembly). For console applications it is an .exe file.
To compile a file in Visual Studio, we press the [F6] key or [Shift+Ctrl+B]. Usually, errors are underlined in red, to attract the programmer’s attention, while we are still writing or when compiling, at the latest. They are listed in the "Error List" window if it is visible (if it is not, we can show it from the "View" menu of Visual Studio).
If our project has at least one error, it will be marked with a small red "x" in the "Error List" window. Short info about the problem is displayed for each error – filename, line number and project name. If we double click any of the errors in the "Error List", Visual Studio will automatically take us to the file and line of code where the error has occurred. In the screenshot above the problem is that we have “using Systema;” instead of “using System”.
Starting the Project
To start the project, we press [Ctrl+F5] (holding the [Ctrl] key pressed and at the same time pressing the [F5] key).
The program will start and the result will be displayed on the console, followed by the "Press any key to continue . . ." message:
The last message is not part of the result produced by the program. It is a reminder by Visual Studio that our program has finished its execution and it gives us time to see the result. If we run the program by only pressing [F5], that message will not appear and the result will vanish instantly after appearing because the program will have finished its execution, and the window will be closed. That is why we should always start our console applications by pressing [Ctrl+F5].
Not all project types can be executed. In order to execute a C# project, it needs to have one class with a Main() method declared in the way described earlier in this chapter.
0 Comments