Basic Program Structure
Part of the Fundamentals section of Coddy's C# journey — lesson 4 of 69.
In C#, code is organized into classes. For now, just know that a class is where you'll write your code - we'll explain more about classes later
The Main method is a crucial part of a C# program because it serves as the starting point of execution.
Here's a simple breakdown of a basic C# program:
public class Program { // Class declaration
public static void Main(string[] args) { // Main method
Console.WriteLine("Hello, Coddy!"); // Output statement
// Write code inside Main
}
}You write your code inside the Main method. The indentation (spaces at the start of lines) is used to make the code easier to read — it shows which code belongs inside which block.
Note: You may also see using System; at the top of some programs. This line appears outside the class and imports built-in functionality — for example, it allows you to use Console.WriteLine. It is not part of the class itself.
Important note: In C#, each statement must end with a semicolon (;). The semicolon is mandatory and tells C# that you've reached the end of a statement. Forgetting to add a semicolon will result in a compilation error.
However, note that code blocks enclosed in curly braces {} (like class and method declarations) don't need semicolons.
Challenge
BeginnerCreate a C# program with a class named Program that contains the Main method. Inside the Main method, write code to output the following text:
This is my first C# program!Cheat sheet
In C#, code must be inside a class. The Main method is the entry point where program execution begins.
Basic C# program structure:
public class Program {
public static void Main(string[] args) {
Console.WriteLine("Hello, Coddy!");
// Write code inside Main
}
}Each statement must end with a semicolon (;). Code blocks with curly braces {} don't need semicolons.
Try it yourself
using System;
public class Program {
// Write your code here
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic Shortcuts5Operators Part 2
Comparison OperatorsLogical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3