Skip to content

C# - Program Structure

Before diving into the building blocks of C#, it’s essential to understand the basic structure of a program. This will serve as a foundation for all the concepts you’ll learn.

1. The Simplest C# Program: Top-Level Statements

Section titled “1. The Simplest C# Program: Top-Level Statements”

Starting with C# 9, you can write simple programs without explicitly defining a namespace, class, or Main method. This is called top-level statements and is perfect for learning, scripting, and small utilities.

Here is the modern “Hello, World!” program in C#:

// This is a complete C# program!
using System;
Console.WriteLine("Hello, Modern C# World!");
// You can define variables and call methods directly.
var name = "Alice";
Console.WriteLine($"Hello, {name}!");

The compiler automatically generates the necessary class and Main method entry point behind the scenes. This structure greatly simplifies the entry into C# programming.

For larger, more organized applications, the traditional structure is used. This structure explicitly defines the key components of a C# program.

// 1. Using Directives: Import namespaces
using System;
// 2. Namespace: Organizes your code
namespace HelloWorldApplication
{
// 3. Class: Contains data and methods
class Program
{
// 4. Main Method: The entry point of the application
static void Main(string[] args)
{
// 5. Statements: Instructions to be executed
Console.WriteLine("Hello, Structured C# World!");
}
}
}
  • Using Directives: The using keyword imports namespaces, giving you access to their types without having to write the full name (e.g., System.Console.WriteLine becomes Console.WriteLine). C# 10 introduced global using to apply a directive to all files in a project.
  • Namespace: A namespace is a container that organizes a set of related classes, structs, interfaces, etc. It helps prevent naming conflicts in large projects. Think of it as a folder for your code.
  • Class: A class is a blueprint for creating objects. It bundles data (fields, properties) and behavior (methods) into a single unit.
  • Main Method: This is the entry point of every C# application. When you run your program, the Main method is the first code to be executed. It must be static.
  • Statements & Expressions: A statement is a single action, like a method call or a variable declaration. Every statement in C# must end with a semicolon (;).
  • Comments: Use // for single-line comments and /* ... */ for multi-line comments. Comments are ignored by the compiler and are used to explain code.

A well-structured program is easier to read, debug, and maintain. Follow these industry best practices:

  • Meaningful Names: Use descriptive names for variables, methods, and classes. Follow the .NET naming conventions: PascalCase for classes and methods, camelCase for local variables.
  • Single Responsibility: Each method and class should have one clear purpose.
  • Code Indentation: Proper indentation makes the code’s structure visually clear.
  • Separate Files: Keep each class in its own file (e.g., Student.cs, Course.cs) to keep your project organized.

Here’s an example with two classes, each in its logical place.

// File: Student.cs
namespace School.Management;
public class Student
{
public string Name { get; set; } = string.Empty;
public int Id { get; set; }
public void DisplayInfo()
{
Console.WriteLine($"Student ID: {Id}, Name: {Name}");
}
}
// File: Program.cs
using System;
using School.Management; // Import the namespace
class Program
{
static void Main()
{
var student1 = new Student
{
Id = 1,
Name = "Alice"
};
student1.DisplayInfo();
}
}

When the above code is compiled and executed, it produces the following result:

Student ID: 1, Name: Alice

The modern way to build C# applications is with the .NET Command-Line Interface (CLI), which works on Windows, macOS, and Linux. Visual Studio and VS Code use this toolchain behind the scenes.

  1. Install the .NET SDK: Download it from the official Microsoft website.
  2. Create a new project: Open your terminal or command prompt and run dotnet new console -o MyFirstApp. This creates a new folder MyFirstApp with a basic console project.
  3. Navigate to the directory: cd MyFirstApp.
  4. Write your code: Open the Program.cs file in a text editor (like VS Code) and write your code.
  5. Run the project: In the terminal, simply run dotnet run. The CLI will compile and execute your application.

Key Points to Remember:

  1. C# is case-sensitive (name and Name are different variables).
  2. Program execution always starts at the Main method (or the first line of top-level statements).
  3. Unlike Java, the C# file name does not need to match the class name.