C# Program Structure

Understanding the building blocks of a C# application

Basic Structure

Every C# program follows a specific structure that the compiler recognizes. The most basic C# program consists of a class with a Main method.

Basic C# Program Structure
using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Key Components

Using Directives
Import namespaces to use types without full qualification
Namespace
Organizes code and prevents naming conflicts
Class
Blueprint for objects containing data and methods
Main Method
Entry point of the application
Statements
Instructions that perform actions
Comments
Explanatory notes ignored by the compiler

Detailed Explanation

1. Using Directives

The using System; directive allows you to use types from the System namespace without specifying the full namespace each time.

2. Namespace Declaration

Namespaces help organize code and prevent naming collisions. The namespace HelloWorld declaration defines a container for related types.

3. Class Declaration

Classes are the fundamental building blocks of C# programs. class Program defines a class named Program.

4. Main Method

The Main method is the entry point of a C# application. It must be static, and it's where the program starts execution.

5. Method Body

The code between the curly braces {} is the method body containing statements that execute when the program runs.

Important Notes

Entry Point Requirement

Every C# application must have exactly one Main method as its entry point. The compiler will generate an error if no Main method is found, or if multiple Main methods exist without specifying which to use.

Case Sensitivity

C# is case-sensitive. "Main" is different from "main". The entry point method must be named "Main" with a capital M.

Extended Example

More Complete C# Program
// Using directives to import namespaces
using System;
using System.Collections.Generic;

// Namespace declaration
namespace CompanyApp
{
    // Class declaration
    class Program
    {
        // Main method - entry point
        static void Main(string[] args)
        {
            // Program execution starts here
            Console.WriteLine("Application started!");
            
            // Check command line arguments
            if (args.Length > 0)
            {
                Console.WriteLine($"Received {args.Length} arguments");
            }
            
            // Call another method
            DisplayMessage();
            
            // Keep console window open
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        
        // Custom method
        static void DisplayMessage()
        {
            Console.WriteLine("Hello from DisplayMessage method!");
        }
    }
}

Program Execution Flow

Understanding how a C# program executes from start to finish

C# Program Execution Flow

Execution Steps:

  1. CLR loads the assembly and looks for the Main method
  2. Main method begins execution
  3. Statements execute sequentially
  4. Methods are called as encountered
  5. Program runs until Main method completes or termination
Back to Dashboard