← Back to C# Course | Chapter 1: Setup & Basics | Lesson 5 of 7

Program Structure

A C# program is organized like a school: namespaces are the buildings, classes are the classrooms, and methods are the lessons inside. Knowing where things go helps you find them later.

In this page:

  1. Program Structure
Syntax
csharp
using System;

namespace NamespaceName
{
    class ClassName
    {
        static void Main(string[] args)
        {
            // statements
        }
    }
}

Program Structure

A C# file is organized into namespaces, classes, and methods, with Main acting as the program's entry point. Statements inside Main execute top to bottom.

Understanding this structure is essential before writing anything more complex than Hello World.

Note: Only one Main method is allowed to be the entry point of an executable project.

Example: Program Structure

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Program started");
        Greet();
        Console.WriteLine("Program finished");
    }

    static void Greet()
    {
        Console.WriteLine("Hello from a helper method!");
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting that a program needs an entry point, such as Main or top-level statements, in only one file.
  2. Putting top-level statements in more than one file, which is a compile error.
  3. Mismatching braces in nested namespaces, classes or methods, giving errors far from the real mistake.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.