C# public class Program Explained
Understand classes, the Main method, objects, access modifiers and the basic structure of a C# program through practical examples.
What Does public class Program Mean?
When you begin learning C#, one of the first structures you may see is:
public class Program
{
}
This simple declaration contains several important ideas. The word public is an access modifier, class tells C# that we are defining a class, and Program is the name of that class.
1. Understanding the public Keyword
public controls accessibility. A public type or member can generally be accessed from other code that can see its containing assembly.
public class Program
{
}
Here, Program is declared as public. This does not mean that every part of the class automatically becomes public. Individual members still have their own access modifiers.
public
Accessible from code that has appropriate access to the type.
private
Accessible only within the containing type.
protected
Used for access within a type and its derived types.
internal
Generally accessible within the same assembly.
2. Understanding class
The class keyword tells the compiler that you are declaring a class.
Think of a class as a design or blueprint. It can define fields, properties, methods, constructors and other members.
public class Student
{
public string Name;
public void Introduce()
{
Console.WriteLine("Hello!");
}
}
In this example, Student is a class. It contains a Name field and an Introduce method.
3. Why Is the Class Called Program?
Program is not a special mandatory keyword in C#. It is simply a common class name used in many traditional examples and console applications.
You could create another class with a different name:
public class Application
{
}
The important part is understanding the role of the class and where the application's entry point is defined.
4. The Main Method
In traditional C# console applications, the Main method is commonly used as the application's entry point.
public class Program
{
public static void Main()
{
Console.WriteLine("Hello, AICircuit!");
}
}
When this traditional program starts, the runtime uses the configured entry point to begin execution.
Breaking the Main method apart
| Part | Meaning |
|---|---|
| public | Access modifier. |
| static | The method belongs to the type rather than an object instance. |
| void | The method does not return a value. |
| Main | The traditional entry-point method name. |
| () | The method currently accepts no parameters. |
5. Why Is Main static?
A static method belongs to the class itself rather than requiring an object instance before it can be called.
public class Program
{
public static void Main()
{
Console.WriteLine("Program started.");
}
}
This allows the entry point to be invoked without first creating a Program object.
6. Your First Complete C# Program
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
The using directive makes names from the System namespace available in the file. Program defines the class, Main provides the traditional entry point, and Console.WriteLine writes text to the console.
7. What Happens When the Program Runs?
The runtime locates the configured application entry point.
The statements inside the method execute in their normal order.
The program sends the requested text to the console.
Execution continues according to the application's remaining logic.
8. Adding Variables
Variables allow your program to store values.
public class Program
{
public static void Main()
{
string name = "Alex";
int age = 20;
Console.WriteLine(name);
Console.WriteLine(age);
}
}
Here, name stores text while age stores an integer value.
string
Used for text such as names, titles and messages.
int
Used for whole-number integer values.
double
Used for floating-point numeric values.
bool
Stores true or false.
9. Creating Your Own Method
A class can contain multiple methods. This helps divide a program into smaller, understandable pieces.
public class Program
{
public static void Main()
{
SayHello();
}
static void SayHello()
{
Console.WriteLine("Welcome to C#.");
}
}
Main calls SayHello. The SayHello method performs the actual output.
10. Methods That Return Values
A method does not always need to be void. It can return a value.
static int Add(int a, int b)
{
return a + b;
}
public static void Main()
{
int result = Add(5, 7);
Console.WriteLine(result);
}
The method returns an integer, so its return type is int.
11. Understanding string[] args
Another traditional Main signature is:
public static void Main(string[] args)
{
Console.WriteLine("Application started.");
}
The args parameter can contain command-line arguments supplied when the application is launched.
It is an array of strings, meaning it can contain multiple text values.
public static void Main(string[] args)
{
foreach (string argument in args)
{
Console.WriteLine(argument);
}
}
12. Classes and Objects
A class describes a type. An object is an instance of that type.
public class Car
{
public string Model;
public void Start()
{
Console.WriteLine("Car started.");
}
}
public class Program
{
public static void Main()
{
Car car = new Car();
car.Model = "Example";
car.Start();
}
}
The Car class defines what a Car object can contain and do. The new keyword creates an instance of the class.
13. Program Class vs Other Classes
There is nothing magical about the name Program. A well-organized application may contain many classes.
| Class | Possible responsibility |
|---|---|
| Program | Application startup or orchestration. |
| Customer | Represents customer-related data and behavior. |
| Product | Represents a product. |
| Calculator | Contains calculation-related operations. |
| FileService | Handles file-related application logic. |
14. Building a Simple Calculator
Let's combine classes, methods, parameters and return values.
public class Program
{
public static void Main()
{
int first = 10;
int second = 5;
int sum = Add(first, second);
int difference = Subtract(first, second);
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Difference: " + difference);
}
static int Add(int a, int b)
{
return a + b;
}
static int Subtract(int a, int b)
{
return a - b;
}
}

