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.

Our program with top-level statements:
        graph TD
    b["var ItemCount = 5;<br>Console.Write(ItemCount);"]
    
Actually these are converted to:
        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.

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:

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(”🙂”).

object-oriented programming

(short OOP) a programming paradigm based on the object. An OOP program consists of objects that interact with one another.

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.

boilerplate code

sections of code that are repeated in multiple places with little to no variation.

Example class Program structure:

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.

  2. If you have functions, prepend them with static and put them inside class Program, but outside Main.

  3. Put rest of the code inside Main.

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 25 (Converting top-level statements to class Program)

  1. Create a new project. In the project creation settings. Activate Do not use top-level statements.

  2. Integrate top-level statements in Activity 21 to a class Program structure.

Appendix#