You can check this course on my website.
So we got to writing the first program, but what do we need for writing program?
- Text editor where we could write the program code.
- Compiler to compile the written code into the application (.exe)
- .NET framework which is required to compile and execute the program. If you want to refresh your memory, reread ‘What is C# and .NET framework’
To solve the problem above we will use Visual Studio 2022, because VS 2022 has free version (Community) and there you can write, test and debug code in one place. In addition, it is very helpful in writing code thanks to the built-in IntelliSense.
Let’s go to Microsoft’s official website and download Community version — Microsoft Visual Studio 2022.
All previous versions of Visual Studio do not support . NET 6 and C# 10, so download Visual Studio 2022.
After downloading, launch the installer. Choose ‘ASP.NET and Web Application Development’ to install C# and .NET 6. You can choose other components (e.g. .NET desktop development), but the more you take the more space on the disk they will take. But don’t worry, at any time you can download the components you need through Visual Studio Installer.
Click ‘Install’ and wait until everything you need is downloaded to your computer.
Now that we’ve set everything up, let’s write Hello World program. Open VS 2022 and choose ‘Create a new project’.
At this stage, select ‘Console App’:
Then enter the name of the project and the directory where it will be located. I will name the project ‘HelloWorldApp’:
Next, select the .NET version that will be used in the project.
After that, Visual Studio will create and open the project for us:
On the screen we see a text editor which is located in a large field in the center where we write the code.
On the right side is the Solution Explorer window where you can see the structure of your project with all dependencies — Dependencies node. Dependencies node contains dll assemblies that are added to the project by default and if you add the libraries you need in the future they will be written to this node, of course unnecessary dependencies can be removed.
Next, in the project structure we see the file Program.cs, which contains the code on C# with following code which generated by default.
The code is pretty self-explained.
First line of code — comment with link to explanations for the code. Second line of code — output text to the console. About Console class we will talk a little bit later.
We already have a simple program even though Visual Studio generated the code itself. Let’s run the program. You can do this by pressing F5 or from the toolbar by clicking on the green arrow.
You should see the following result — opened console and text ‘Hello, World!’
After compilation, the application (.exe) can be found on the hard disk in the project folder in the bin\Debug\net6.0 directory. It will be named like the name of the project and application can be ported to other platforms where .NET 6 is installed.
Now let’s improve our program and add a couple of lines ourselves:
The program asks our name and then greets us:
The first line simply outputs text to the console as in the example above:
Here we declare a string variable and write the value that the user entered in console (Console.ReadLine()). Sign ? after the name of the variable indicates that the variable can be null, that is, do not store any value.
Then the name entered is printed to the console:
With the dollar sign ($) and curly braces — string interpolation, we put the name variable in the string, thus, name will be substituted with the value entered by the user into the console.
It’s time to test it, press F5 or green arrow in the toolbar.
My result:
That’s it, we created the first application and learned how to compile and run it in the console.
Previous article -> 1.2 Evolution of C#
Next article -> 1.4 Debugging
Join to our community in Telegram — https://t.me/itifico
If you want to support me, you can buy me a cup of coffee and I will drink it when I write the next article :)
Donations list:
- Dinesh Chintalapudi — 3$
- Unknown — 5$