# First project

We will run our first program in this section.

## Setting the default project folder

Rider creates new projects under `RiderProjects` in your home folder as default. I recommend changing the default folder to a subfolder in your course-related folder, e.g., `25fall/indprog/coding-projects`.

:::{tip}
Do not create projects in a iCloud synchronized folder on a Mac. This may lead to compiling errors which change every time you compile your project and also slow compiling times. Alternatively [keep your projects available locally](https://apple.stackexchange.com/questions/450948/macos-keep-individual-files-or-folders-available-offline-within-an-icloud-folde).
:::

1. On the welcome screen, click on `Configure` on the bottom left corner, then `Settings`. Settings window should pop up.
1. Search for `default project dir`. The setting is under `Appearance & Behavior` -> `System Settings` -> `Default project directory`.
1. Click on the folder icon to change it.
1. Click on `Save`. Settings will be closed.

(creating-a-project)=
## Creating a project

1. On the welcome screen, click on {{AllIcons_expui_welcome_createNewProjectTab}} `New Solution`. A new window will pop up with `Project Type` as `Console`.

:::{wpd} 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). Later we will also create GUIs.

1. If this is your first project, you have to install the .NET framework first. Click on `Install`. After the installation you should see additional options. Leave them as they are.
1. We will create an online currency converter. Write into the `Solution name`: `FirstProject`. Project name will be automatically the same.
1. Activate:
   - `Put solution and project in the same directory`
   - `Create Git repository`
1. Click on `Create`. The editor window will show up with `Program.cs`.
1. If a pop up window with `IDE project settings can be added to Git` shows up, click `Don't ask again`.
1. If the `What's new ...` tab shows up, you can close it.

We will introduce Git later.


## Running a program with debugging features

We will introduce the term *debugging* later.

1. Press <kbd>F5</kbd> or {{AllIcons_expui_run_debug}} icon on the top right.

   This step will start the compiler which translates the code to a format that can be executed by our computer. It will be run with debugging features, which we will talk about in a bit. In the end a console window with `Hello, World!` message will pop up in the `Console` tab at the bottom of your screen.

:::{wpd} compiler
a computer program that translates program code written in one programming language into another
:::

Our program has finished running at this point.

## Debugging a program

:::{wpd} 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:

:::{code-block} csharp
:linenos:

Console.WriteLine("Before");
int[] numbers = [1, 2, 3];
Console.WriteLine(numbers[5]);
Console.WriteLine("After");
:::
1. Press <kbd>F5</kbd>. It may take some time if its the first time.

   Then the editor will open a new file `ThrowHelpers.cs` and stop at the line `throw new IndexOutOfRangeException()`.

This line is where our program has stopped according to the debugger. This is not very helpful for us, because the file does not seem to be written by us. We will for now configure Rider to not jump into external files that we don't know about (but stay in our file).

1. Click on Gear icon on the top right corner → Settings. 
1. Search for the `enable external source` option on the top. It will be found under `Debugger` settings.
1. Deactivate it. Click on `Save`. This will close the settings window.

Now we can start debugging again using <kbd>F5</kbd>.

1. The program should stop at third line. The IDE is in debugging mode and will show you actions like {{AllIcons_expui_run_resume}} (resume program), {{stop}} (stop debugging).

   The IDE 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 demonstrating the debugger. An idea is that we check the content of the array `numbers`, *exactly at the time* when the error happened.
   
   This is a superpower of a debugger: we can *pause* our program and inspect contents of data structures, or *computer memory* in general. In this case, our program was automatically paused by the debugger.

   :::{figure} ../img/debugging-numbers-array-rider.png
   :name: debugging-numbers-array-rider
   :align: right
   :figwidth: 40%
   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.
   ::

1. Hover your mouse over `numbers` on the editor, you see first the size of the array as in {numref}`debugging-numbers-array-rider` and if you expand using the > symbol, you see its contents.

   We see that the array has three elements with indexes 0, 1, and 2. However we used the index 5 which does not exist. We can fix our code by adding more elements to the array or using an index of an existing element, e.g., 2.
1. Stop debugging by using <kbd>Shift</kbd><kbd>F5</kbd> or {{stop}}.
1. Change the index from 5 to 2 and restart the debugger by pressing <kbd>F5</kbd>.

   You will see that our program runs and exists without any errors.
   
## Running a program without debugging features

There is a second option to start our program – without debugging.

1. On the left of the {{AllIcons_expui_run_debug}} button, you will see another play button <span style="color: green;">▷</span>, which runs the program without debugging features.

  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 for now and can simply use <kbd>F5</kbd> for starting our program. When we start a typical program on our operating system, it is started without debugging features.

## Wrap-up

You created your first program and used the debugger. We will create a new project in the next section.