# `class Program` and `Main()`

Until now we used *top‑level statements*—statements that sit at the top level of the file and are executed immediately.
The compiler automatically wraps them in a `Program` class and generates a `Main` method.
::::{grid} 2
:::{grid-item-card} Our program with top-level statements:
```{mermaid}
graph TD
    b["var ItemCount = 5;<br>Console.Write(ItemCount);"]
```
:::

:::{grid-item-card} Actually these are converted to:
```{mermaid}
graph TD
  subgraph a[class Program]
  subgraph f["static void Main(string[] args)"]
    b["var ItemCount = 5;<br>Console.Write(ItemCount);"]
  end
  end
```
:::
::::

This is because in C#, unlike in Python or C++, every executable code must belong to a type (and thus object). When we run a program, a code is required to execute. This code is the `Main` function (better called *method*) and `Main` belongs to the type (or class) `Program`.

This type is a `class` in C# by convention. <!--could be also a struct-->

(fundamental-concepts-oop)=
## Fundamental concepts in object-oriented programming

In C#, everything is an object. In next sections we will work more with objects, so we introduce some related fundamental concepts:

:::{wpd} method (computer programming)
a procedure (we called this a function now) associated with an object.
:::

For example, `Write()` is a method in `Console.Write("🙂")`.

object
: a software entity that consists of *state data* (variables) and *behavior* (functions).

For example, `Console` is an object in `Console.Write("🙂")`.

:::{wpd} object-oriented programming
(short OOP) a programming paradigm based on the *object*. An OOP program consists of objects that interact with one another.
:::

:::{wpd} class (computer programming)
defines the shared aspects of objects created from the class.
:::

This was a design decision by C# designers and we have to follow this.

We used top-level statements until now, because these:
- remove the complexity for people starting to learn to code
- remove the need for writing the same code again and again (called *boilerplate code*), if we want to try something or explore.

:::{wpd} boilerplate code
sections of code that are repeated in multiple places with little to no variation.
:::

Example `class Program` structure:

```cs
namespace ConsoleApp1;

internal class Program
{
    private static void Main(string[] args)
    {
        Color agent = Color.Red, human = Color.Green;

        Console.WriteLine($"agent: {agent}");
        Console.WriteLine($"human: {human}");
    }

    private enum Color
    {
        Red,
        Yellow,
        Green
    }
}
```

We will work with code which has a `class Program` (or OOP-based in general) structure. How do we integrate code into this structure? Follow these rules:
1. If you have types like `record` or `enum` put them inside the `class Program`, but outside `Main`. After you apply autoformatting, they will be automatically prepended with `private`.
1. If you have functions, prepend them with `static` and put them inside `class Program`, but outside `Main`.
1. Put rest of the code inside `Main`.

<!--TODO static ?-->

:::{tip}
Use:
1. top-level statements for small programs or quick exploration
2. `class Program` if you use OOP, for consistency with the rest of your code.
:::

Visual Studio and Rider has an option for using/deactivating top-level statements during project generation. If you do not use top-level statements, then the project template will have `class Program` structure.

:::{activity} Converting top-level statements to `class Program`
1. Create a new project. In the project creation settings. Activate `Do not use top-level statements`.
1. Integrate top-level statements in {ref}`personal-data-check-mail-template` to a `class Program` structure.
:::

<!--TODO too much explanation. They should have more questions-->

## Appendix

- [Tutorial: Explore ideas using top-level statements to build code as you learn](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/top-level-statements)