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.
graph TD
b["var ItemCount = 5;<br>Console.Write(ItemCount);"]
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.
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.
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:
If you have types like
recordorenumput them inside theclass Program, but outsideMain. After you apply autoformatting, they will be automatically prepended withprivate.If you have functions, prepend them with
staticand put them insideclass Program, but outsideMain.Put rest of the code inside
Main.
Tip
Use:
top-level statements for small programs or quick exploration
class Programif 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)
Create a new project. In the project creation settings. Activate
Do not use top-level statements.Integrate top-level statements in Activity 21 to a
class Programstructure.