Tools#
We’ll now install Visual Studio 2022 Community, which we’ll refer to as VS, together.
For the following tutorial I used a computer with an Intel x86-64 bit processor with Windows 11 OS build 26100 (refer to System Information
). Even the following steps should also work for other processors like ARM, I could not test.
VS does not support macOS. Use VirtualBox and use Windows as a virtual machine on macOS.
If the following steps do not work out for you, we can find a solution in the class. If you miss something in the steps or found an error, please create an issue or a pull request to help other students.
Visual Studio 2022 Community Edition installation#
Open
Microsoft Store
.Search for
Visual Studio
and selectVisual Studio Community 2022
(notVisual Studio Code
). A purple white installation page will show up.Click on install. It will download the installer which is few MB. A
User Account Control
window will pop up and ask for permission.Click
Yes
.Visual Studio Installer
window will pop up.Click
Continue
. The installer will start downloading components. In the end, a new windowInstalling - Visual Studio Community ...
will pop up and will then show you installable components calledWorkloads
.Scroll down
Desktop & Mobile
and make a tick on.NET desktop development
. On the right,Installation details
should show up.Leave the default components active. Click on
Install
. The installer will start downloading and installing. It took about 10 minutes on my computer. Look into Introduction C# until the installation ends.After the installation a window with
Sign in to Visual Studio
should pop up.You can close the
Visual Studio Installer
andMicrosoft Store
window.Go back to the VS. We will organize our code on repositories and also experiment with GitHub Copilot. So, click on
Sign in with GitHub
. A web-browser will pop up. Login with your credentials or create an account.During login, GitHub will ask you for permissions to exchange data with VS. Accept them. The web-browser will forward you to VS again and you will see
Personalize your Visual Studio experience
.You can close the formerly opened web browser window.
Go back to VS. Click on the
Development settings
drop-down menu and selectVisual C#
.Pick a color theme that fits your environment and your taste. A study found out that a light theme is best in a well-lit environment during the day, and dark during the night.
Click on
Start Visual Studio
. A welcome window should pop up withOpen recent
andGet started
.
Creating a project#
On the welcome window, click
Create a new project
. A list of templates will show up.Double click on
Console App
, which should be the first one on the list.We will create an online currency converter. Name the project
CurrencyConverter
.Activate
Place solution and project in the same directory
. This will create less directories which is more convenient for small projects for us as beginners.Click on
Next
.Additional information
window will show up.You can leave the three settings in their defaults.
If you are curious about these settings:
Typically every .NET version comes with new language features. The framework can stay at the default, because we don’t need the latest language features as beginners.
Do not use top-level statements
: Top-level statements are in contrast to object-oriented programming (OOP). In short, top-level statements allow us to write minimal code, but OOP is more modular. We will introduce these concepts later in detail. We don’t activate this, because we will also use top-level statements.Enable native AOT publish
: AOT stands for ahead of time compilation. Typically Visual C# programs are translated to an intermediate language, which is then translated to machine code first time the program is run. AOT may lead to more efficient programs in small computers and we don’t need this feature in our case.
Click on
Create
. A progress window will pop up and after that the editor window withProgram.cs
containing a hello-world program.
Running a program#
Click on
CurrencyConverter
on the toolbar or F5.This step will start a compiler which translates the code to a format that can be executed by our computer. It will be run with debugging features. Then a console window with
Hello, World!
message will pop up.
- console application
a program designed to be used via a text-only user interface
We will use console applications for learning fundamental programming concepts, because a console application has less complexity compared to a graphical user interface (GUI).
- compiler
a computer program that translates program code written in one programming language into another
Debugging a program#
- debugging
the process of finding the causes and possible fixes for bugs
When our programs get more complicated, debugging will be very useful to find the bugs. In debugging mode, the code will pause at the program line where the first error happened at will give us the opportunity, e.g., which values the variables have to inspect the crime scene like a detective 🕵️.
To learn how debugging works, copy the following code and replace Program.cs
on your editor:
1Console.WriteLine("Before");
2int[] numbers = { 1, 2, 3 };
3Console.WriteLine(numbers[5]);
4Console.WriteLine("After");
Press F5
The program should stop at third line. VS is not debugging mode and will show you actions like ⤍/ (show next statement),
(stop debugging).
VS should show you the error cause
Index was outside the bounds of the array.
. How can this happen? Let us overlook for a moment that we initialized the array with three elements for the sake of this tutorial. We should check the contents of the arraynumbers
.Fig. 1 When we hover our mouse over a data structure in debugging mode, we can see information about its contents. When we click on the ▾ symbol, we can further expand and see actual contents.#
In debugging mode, we can inspect contents of data structures.
Hover your mouse over
numbers
in the editor, you see first the size of the array as in Fig. 1. Now the error cause should be clear and you can fix your code, e.g., using the right index or making sure that the value that[5]
points to exist.On the right of the last button, you will see another play button ▷, which stands for
Start Without Debugging
. Our program will start faster and execute probably also faster in this mode, because debugging features also affect performance.We don’t care about performance right now and can simply use F5 for starting our program.
Close the console by pressing a key. You should be back on your editor.
Saving our code on a repository#
Now we will store our code on a repository which is an important tool in version control systems.
- version control
the software engineering practice of controlling, organizing, and tracking different versions in history of computer files; primarily source code text files, but generally any type of file.
- repository
(in context of version control systems) a data structure that stores metadata for a set of files or directory structure. The main purpose of a repository is to store a set of files, as well as the history of changes made to those files.
Keeping a history of changes is helpful if we or someone else who also work in the project would like to understand changes in the project which is useful for collaboration. Keeping a history helps also to roll changes back if something does not work as intended.
Keeping projects in a repository is also helpful to collaborate and share code. You will use it later to share or submit your projects. You probably already downloaded something from the code forges GitHub or GitLab.
- code forge
a web interface to a version control system
Note that repositories are not only good for program code but for all kinds of text-based files.
We had already logged in on GitHub with VS, so let us create our first repository on GitHub.
On the right you should see a sidebar with the title
GitHub Copilot Chat
. On the bottom of that sidebar, click on the tabGit Changes
. You should now seeGit Changes tab
.Click on
Create Git Repository
. A new window with the same name will pop up.If you want to share your project, e.g., for submitting homework using a URL:
activate
Add a README.md
to add information about your project.Change visibility to
Public
.
Click
Create and Push
. You may be asked whether you want to save your files before pushing.During this process your repository will be created.
If you get the error
error setting cerificate file: ...
, refer to here.If you want to push your code again, e.g., in case of an error, then use ⬆️ on the Git bar.
If successful, you should see
ℹ️ Successfully pushed ...
.Now check how your pushed code look like on the code forge GitHub by clicking on
...
on theGit changes
bar and clicking onOpen in Browser
or simply browsinghttps://github.com/*YOUR-USERNAME*/CurrencyConverter
.You should see something similar to:
user and user Add project files. ... 2 Commits .gitattributes .gitignore CurrencyConverter.csproj CurrencyConverter.sln Program.cs README.md
Files beginning with
.git
are for Git configuration. These are templates from VS that we don’t need to understand right now.*.csproj
and*.sln
are project and solution files. VS takes care of them when we save our project.Program.cs
is our source codeREADME.md
contains information about our project that we will fill with info later.
- source code
a plain text computer program written in a programming language
- README
contains info about files in a directory of computer software. It is a form of documentation.
On GitHub, click on the 2 Commits
.
Tip
If you cannot find a word or identifier from this tutorial immediately on the web page, use Ctrlf to search for it.
- changeset
list of differences between two successive versions in a repository
- commit
the operation of committing such a changeset to the repository
VS automatically created two commits and added some information message to each commit. When we use repositories we make our changes in commits.
Ideally each commit should be a set of changes that adds one or many describable feature like improved user name handling or added robot program. The advantage is that the programmer can roll back changes if these features led to problems later. But often people don’t want to put so much structuring work and use a repository as a diary and commit code at the end of the day.
You see that the commits are associated with user
which should be the username of your operating system account. Let us change it to our GitHub username:
Click on the top menu on
Git
-> ⚙️Settings
.Options
window should pop up.Enter your GitHub username and email.
To test, make a small change in your code like adding a new line to your code (Enter).
Now save your file using Ctrls. You should see a change on the
Git bar
in theChanges
list. This will list your changes compared to the last commit.Without any change, we cannot create a commit
On the
Git Changes
bar, write a test message liketesting new username
onto the boxExter a message
.Click on
Commit All
. You should seeCommit ... created locally
, which means that we created a commit on our local project, but the code forge is not aware of our changes right now.If you see
ℹ️ Nothing to commit
, then you probably did not change any file.Fig. 2 GitHub commit after setting the username correctly#
Click on ⬆️ to synchronize our repository with the repository on the code forge GitHub.
Check the commits on your web web browser. Now you should see your username like in Fig. 2.
Improvement suggestions with the Screwdriver (
) and traditional C# style#

Fig. 3 Screwdriver gives typically improvement suggestions#
On the default template code, you should see the screwdriver icon on the top. This gives you typically improvement suggestions that you can use as seen in Fig. 3.
Click on the
and click on the suggestion about
Convert to 'Program.Main' style program
. You will see additional code that wraps the original code as follows:internal class Program { private static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }
Press F5. You should see the same program output.
This is the traditional C# code that shows the actual structure of a top-level program. Top-level means the entry point (to the program) – the functions that first get executed when we start a program.
But why would you write code that is serves no functional purpose? As long as we don’t have to use this functionality somewhere else, there is no need to write class declarations. But as software evolves with time, this functionality may become a component of a more complex program, which means that the top-level may change. In this case we have to introduce such a class structure. Nevertheless, we will prefer top-level statements to traditional top-level C# code to simplify our code.
If you encounter C# code on the internet, you may see both versions, so it is important to know the difference.
Last but not least, suggestions may not always improve your code. So be critical.
A currency converter with input & Code style#
Let us finally try a more meaningful program — a currency converter. We will also talk about code style Replace Program.cs
with the following:
1using System.Text.Json;var client=new HttpClient ( );
2string json=
3await client.GetStringAsync("https://api.frankfurter.dev/v1/latest?base=DKK");
4var dkk_in_eur = JsonDocument.Parse(json).RootElement.GetProperty("rates").GetProperty("EUR").GetDouble();
5
6Console.WriteLine( $"1 DKK = {dkk_in_eur} EUR");
7Console.Write("Enter amount in DKK: ");
8
9var dkk_amount = Convert.ToDouble(Console.ReadLine());
10var eur_amount = dkk_amount * dkk_in_eur;
11Console.WriteLine($"{dkk_amount} DKK = {eur_amount:F2} EUR");
Run the program using F5.
Try the program with an input.
Now let us speak about code style. Look at the highlighted lines. Does something catch your eye? Think about it some seconds before you continue.
If you have the feeling that the code is not easy to read, then you are on the right track! When we write ordinary text, then we pay attention that the words that belong to a sentence belong to the same line unless we continue on the next line as follows:
The program converts between DKK and EUR.
Now look at the following:
The
program
converts
between DKK and EUR.
Both text have the same meaning, but the former is more readable. When we write programs we also pay attention to programming style.
- programming style
also known as coding style, are the conventions and patterns used in writing source code, resulting in a consistent and readable code
The style mistakes in the first lines are:
A typical statement ends with a semicolon
;
. Every statement is typically on a separate line.We use spaces around
=
.We don’t leave spaces after a function name and between empty parentheses:
HttpClient()
.We typically separate lines that logically do not belong together, e.g.,:
using System.Text.Json; var client = new HttpClient();
Additionally we could also separate
Console.WriteLine
functions for better readability, as they print something on the terminal.A similar analogy would be writing paragraphs in written text.
We humans may not behave consistently and make mistakes but machines typically do more consistent work. Fortunately, editors can automatically format the code for us:
Click on the first line. An icon similar to
should appear in the beginning of the line. Click on
, then a menu will come up.
Click on
Fix Formatting
. You will see that most of the style issues are fixed, but not all. The formatter cannot understand everything in our code, so we have to still may attention to writing “paragraphs” in our code.
Instead of formatting our code every time manually, let us format our code whenever we save our project:
Tools
->Options
->Text Editor
->Code Cleanup
Activate
Run Code Cleanup profile on Save
and click onOK
.Save your document Ctrls. You should see that the formatting on line 7 is fixed too.
Using Copilot as an assistant#
A GPT can support us better than editor assistance tools during coding, because a GPT can have a deeper understanding. As an example, we will use the GPT Copilot for formatting and explaining the code.
On the bottom of the right bar, click on the
GitHub Copilot Chat
.Ask Copilot:
format code
. Here is the output from ChatGPT 4.1:using System.Text.Json; var client = new HttpClient(); string json = await client.GetStringAsync("https://api.frankfurter.dev/v1/latest?base=DKK"); var dkk_in_eur = JsonDocument.Parse(json) .RootElement .GetProperty("rates") .GetProperty("EUR") .GetDouble(); Console.WriteLine($"1 DKK = {dkk_in_eur} EUR"); Console.Write("Enter amount in DKK: "); var dkk_amount = Convert.ToDouble(Console.ReadLine()); var eur_amount = dkk_amount * dkk_in_eur; Console.WriteLine($"{dkk_amount} DKK = {eur_amount:F2} EUR");
We see that ChatGPT not only added a nice wrapping on the fifth line (VS can also do this with
, but it is manual anyway) but also separated our code logically by separating the header (
using System...
), getting and parsing the data (second block) and outputting the data (last block).Apply these suggestions by clicking
Apply
on the top of the suggestions. VS may ask you to accept some changes with Tab.Try the program again to be sure that it still works.
You probably used ChatGPT for explaining you things. Let us try this too.
Ask Copilot:
this is my first programming language. Explain me the code
. You should see explanations line by line.
Tip
One of the learning goals of this course is to identify and explain core programming concepts without a GPT to be able to criticize the output of a GPT. I recommend you to write the programs yourself and ask the GPT only for getting feedback, improvements or explanations.
Lastly let us finish this project for now by writing minimal documentation to README.md
and committing our changes:
On the bottom of the right bar, click on
Solution Explorer tab. Open
README.md`.Under the title
# CurrencyConverter
, write, e.g.,An online currency converter from DKK to EUR.
Save
README.md
.Go to the
Git Changes
tab.Try to think about a message which summarizes your commit before you continue to read.
I came up with:
initial currency converter
Now try generating a commit message using Copilot by pressing the pen icon similar to 🖊️. I got the following message:
Add DKK to EUR conversion functionality Updated `Program.cs` to implement currency conversion from Danish Krone (DKK) to Euro (EUR) using an external API. The program now fetches exchange rates, prompts the user for an amount in DKK, and displays the equivalent amount in EUR. Modified `README.md` to include a description of the program as an online currency converter.
This is more detailed and correct. You have to choice whether you accept it or not.
Commit your changes.
Push your changes.
Summary#
You installed and used VS and its helpful components on example code, congratulations 🎉! Now you are ready to dive deeper in C# coding.
Overview of the shortcuts we have used#
Ctrls |
save project |
F5 |
compile and run project in debugging mode |
Ctrlf |
search |