New Batch Starts 1st September 2025 – Join Our .NET Full Stack Development Course, Daily 6-8 AM and 8.30-10.30 PM, Register Now
+91 9989950088
info@example.com
Basic Structure of the C# Program

πŸ“˜ Basic Structure of a C# Program


A C# program is made with some fixed parts. It always starts from the Main method. The simple example is Hello World program which shows message on the screen.

  • Namespace – Like a folder to keep classes.
  • Class – Like a box which holds methods and variables.
  • Class Members – Items inside a class (variables, methods).
  • Main Method – Starting point of the program (runs first).
  • Statements – Actual instructions written for the program.
  • Comments – Notes only for reading, computer ignores them.

βœ… All these together make the base structure of a simple C# program.

πŸ“˜ Basic Flow of a C# Program


1️⃣ Namespace

At the top, we start with a Namespace. Think of a namespace like a folder on your computer, where related files are kept together. In C#, namespaces organize classes and prevent naming conflicts.

namespace HelloWorld

This means our program is grouped under the HelloWorld namespace.

2️⃣ Class

Inside the namespace, we create a Class. A class is like a blueprint or a box that holds data and methods.

class Program

Here, the class is named Program, which will contain our code.

3️⃣ Class Members

Inside the class, we have Members such as variables, methods, or properties. In our example, we have the Main method as a member:

static void Main(string[] args)

4️⃣ Main Method

The Main method is the starting point of every C# program. When we run the program, execution begins from here.

Console.WriteLine("Hello, World!");

This displays Hello, World! on the screen.

5️⃣ Comments

Comments are notes written for understanding the code, but they are not executed by the computer.

// This is the main method
βœ… Flow of a C# program:
Namespace β†’ Class β†’ Members β†’ Main Method β†’ Comments

Together, these form the foundation of every C# application.
Program Demonstration
C# Program: Hello World
using System;

namespace HelloWorldApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // This will print Hello World on console
            // Added Welcome message also
            Console.WriteLine("Hello World!");
            Console.WriteLine("Welcome, User!");
        }
    }
}
        
Hello World! Welcome, User!

1. using System; β†’ This line allows us to use basic classes like Console.

2. namespace HelloWorldApp β†’ A namespace is like a folder where your program code is kept safe.

3. class Program β†’ All C# code runs inside a class. Here the class name is Program.

4. static void Main(string[] args) β†’ This is the starting point of every C# program. The computer starts running code from here.

5. Console.WriteLine("Hello World!"); β†’ This prints Hello World! on the screen.

6. Console.WriteLine("Welcome, User!"); β†’ This prints Welcome, User! on the screen.

Final Output:
Hello World!
Welcome, User!

C# Program: Namespace Declaration
using System;

namespace MyFirstNamespace
{
    class Greeting
    {
        static void Main(string[] args)
        {
            // Program demonstrating Namespace Declaration
            Console.WriteLine("Hello from MyFirstNamespace!");
        }
    }
}
        
Hello from MyFirstNamespace!

1. using System; β†’ Allows us to use built-in classes like Console.

2. namespace MyFirstNamespace β†’ A namespace groups related classes and avoids name conflicts.

3. class Greeting β†’ A class that contains our program logic.

4. static void Main(string[] args) β†’ The entry point of the program, where execution begins.

5. Console.WriteLine("Hello from MyFirstNamespace!"); β†’ Prints a message to the screen.

Final Output:
Hello from MyFirstNamespace!

C# Program: Using Directives (using)

using System;  // Using directive lets us access Console directly

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
        Console.WriteLine("Using 'using' directive to access Console directly.");
    }
}

Hello, World! Using 'using' directive to access Console directly.

1. using System; β†’ Lets us use classes like Console without typing System.Console.

2. class Program β†’ Defines a class where our program runs.

3. static void Main() β†’ Entry point of the program. Execution starts here.

4. Console.WriteLine(...); β†’ Prints messages on the screen.

Final Output:
Hello, World!
Using 'using' directive to access Console directly.

C# Program: Class Declaration

using System;

class Car
{
    public string brand = "Honda";

    public void ShowBrand()
    {
        Console.WriteLine("Car Brand: " + brand);
    }
}

class Program
{
    static void Main()
    {
        Car myCar = new Car();
        myCar.ShowBrand();
    }
}

Car Brand: Honda

1. class Car β†’ Defines a blueprint for creating Car objects.

2. public string brand = "Honda"; β†’ A field (variable) inside the class storing the brand name.

3. public void ShowBrand() β†’ A method that prints the car brand.

4. class Program β†’ The class that contains the Main() method.

5. Car myCar = new Car(); β†’ Creates an object of the Car class.

6. myCar.ShowBrand(); β†’ Calls the method to display the car brand.

Final Output:
Car Brand: Honda

The Main() Method in C#

Program Name:

MainMethodExample.cs

Source Code:
using System;

class Program
{
    // Entry point of the program
    static void Main()
    {
        Console.WriteLine("Hello, this is the Main() method in action!");
    }
}
          
Program Output:
Hello, this is the Main() method in action!
Explanation:

In C#, the Main() method is the entry point of every program. When the application runs, execution begins from the Main() method.

  • static β†’ Means it can run without creating an object of the class.
  • void β†’ Specifies that the method does not return any value.
  • Console.WriteLine() β†’ Prints output on the console.

Statements & Expressions in C#

StatementsExpressionsExample.cs
using System;

class Program
{
    static void Main()
    {
        // Statements: declarations + assignments
        int a = 5;               // declaration statement with initialization
        int b = 10;

        // Expressions: produce values
        int sum = a + b;         // arithmetic expression
        int product = a * b;
        bool isAGreater = a > b; // relational expression

        a += 3;                  // compound assignment statement (a = a + 3)
        int afterIncrement = ++b; // prefix increment expression

        Console.WriteLine($"a = {a}, b = {b}");
        Console.WriteLine($"Sum = {sum}, Product = {product}");
        Console.WriteLine($"Is a > b? {isAGreater}");
        Console.WriteLine($"afterIncrement = {afterIncrement}");
    }
}
a = 8, b = 11
Sum = 15, Product = 50
Is a > b? False
afterIncrement = 11

What’s happening here?

1. Declarations & Assignments: int a = 5;, int b = 10; are statements that create variables and assign values.

2. Arithmetic Expressions: a + b and a * b produce values used to initialize sum and product.

3. Relational Expression: a > b evaluates to a boolean (false here) and is stored in isAGreater.

4. Compound Assignment: a += 3 updates a (now 8)β€”this is a statement that contains an expression.

5. Increment Expression: ++b increments first, then returns the value (11), stored in afterIncrement.

6. Output Statements: Console.WriteLine(...) are statements; inside them we embed expressions via string interpolation.

Key idea: Statements perform actions; expressions produce values. Most statements are built from expressions.

Access Modifiers in C#

Program: AccessModifiersDemo.cs

using System;

class Person
{
    public string Name = "Ravi";        // Public - Accessible everywhere
    private int age = 25;              // Private - Accessible only inside this class
    protected string Address = "Hyd";  // Protected - Accessible inside this class & derived classes
    internal string Email = "ravi@example.com"; // Internal - Accessible within same project

    public void ShowDetails()
    {
        Console.WriteLine("Name: " + Name);
        Console.WriteLine("Age: " + age);
        Console.WriteLine("Address: " + Address);
        Console.WriteLine("Email: " + Email);
    }
}

class Student : Person
{
    public void ShowStudent()
    {
        Console.WriteLine("Student Name: " + Name);      // Public accessible
        Console.WriteLine("Student Address: " + Address); // Protected accessible
    }
}

class AccessModifiersDemo
{
    static void Main()
    {
        Person p = new Person();
        p.ShowDetails();

        Student s = new Student();
        s.ShowStudent();
    }
}
        
Name: Ravi
Age: 25
Address: Hyd
Email: ravi@example.com
Student Name: Ravi
Student Address: Hyd
        

Access Modifiers in C# define the visibility of members (variables, methods, classes). In this program:

  • public: Accessible everywhere (e.g., Name).
  • private: Accessible only inside the same class (e.g., age).
  • protected: Accessible inside the class and derived classes (e.g., Address).
  • internal: Accessible only within the same project/assembly (e.g., Email).

This helps in encapsulation by controlling access to data.

Organizing a C# Program

Program: StudentInfoDemo.cs


using System;

namespace School
{
    class Student
    {
        public string Name;
        public int Age;

        public void DisplayStudentInfo()
        {
            Console.WriteLine("Student Name: " + Name);
            Console.WriteLine("Student Age: " + Age);
        }
    }

    class Program
    {
        static void Main()
        {
            Student student1 = new Student();
            student1.Name = "Alice";
            student1.Age = 14;

            student1.DisplayStudentInfo();
        }
    }
}

Student Name: Alice
Student Age: 14
        

A well-structured C# program is easier to read, debug, and maintain. In this program:

  • namespace School: Groups related classes together.
  • class Student: Represents a real-world student with Name and Age.
  • DisplayStudentInfo(): Prints student details. Short & focused method.
  • class Program β†’ Main(): Program starts here. Creates a Student object and calls its method.

Best Practices:

  • Use proper indentation for readability.
  • Meaningful names: Student, DisplayStudentInfo.
  • Follow naming conventions: PascalCase for classes & methods, camelCase for variables.
  • Keep methods short and focused on one task.
Using Visual Studio:
  1. Open Visual Studio.
  2. Go to File β†’ New β†’ Project β†’ Console App.
  3. Paste the code in Program.cs.
  4. Press F5 to run β†’ Output appears in the console.
Using Command-line:
  1. Save file as StudentInfoDemo.cs.
  2. Open Command Prompt.
  3. Navigate to file directory.
  4. Run β†’ csc StudentInfoDemo.cs (compiles the code).
  5. Run β†’ StudentInfoDemo (executes the program).

C# Program Structure Summary

  • Namespace: Groups related classes to avoid naming conflicts. namespace HelloWorldApp
  • Class: Blueprint or container for variables, methods, and properties. class Program
  • Class Members: Variables and methods inside a class. static void Main(string[] args) is the entry point.
  • Comments: Help understand code, ignored during execution. // Single-line, /* Multi-line */
  • Access Modifiers: Control visibility of class members. Examples: public, private, protected, internal
  • Statements & Expressions: Statements perform actions; expressions produce values. using System; simplifies access to classes like Console.
Key Takeaway: Organizing a C# program with namespaces, classes, members, statements, and comments ensures readability, maintainability, and scalable code.