What are the main parts of a C# program?

The main parts of a C# program include −

  • Namespace declaration
  • A class
  • Class methods
  • Class attributes
  • A Main method
  • Statements and Expressions
  • Comments

Understanding these components is essential for writing effective C# programs. Each part serves a specific purpose in organizing and executing your code.

Basic C# Program Structure

The following example demonstrates a simple C# program with all the essential parts −

using System;

namespace Demo {
   class Program {
      // Class attribute (field)
      static string message = "Our first program in C#!";

      // Main method - entry point
      static void Main(string[] args) {
         // Method call statement
         DisplayMessage();
      }

      // Class method
      static void DisplayMessage() {
         Console.WriteLine(message);
      }
   }
}

The output of the above code is −

Our first program in C#!

Breakdown of Program Components

C# Program Structure using System; Namespace import namespace Demo { } Namespace declaration class Program { } Class declaration static string message; Class attribute/field static void Main() { } Main method (entry point) Console.WriteLine(); Statement/Expression Nested structure: Namespace ? Class ? Methods ? Statements

Using Statement

The using System; directive includes the System namespace, which contains fundamental classes like Console. Multiple using statements can be added for different namespaces.

Namespace Declaration

A namespace is a collection of related classes. The Demo namespace contains the Program class. Namespaces help organize code and prevent naming conflicts.

Class Declaration

The class Program defines a container for data (attributes) and methods. Classes are the fundamental building blocks of object-oriented programming in C#.

Main Method

The Main method is the entry point where program execution begins. It must be static and can accept command-line arguments through the string[] args parameter.

Program with Comments

using System;

namespace StudentApp {
   // Student class with attributes and methods
   class Student {
      // Class attributes
      public string name;
      public int age;

      // Constructor method
      public Student(string studentName, int studentAge) {
         name = studentName;  // Assign parameter to field
         age = studentAge;
      }

      // Display method
      public void ShowDetails() {
         /* Multi-line comment:
            This method displays student information */
         Console.WriteLine($"Name: {name}, Age: {age}");
      }

      // Main method - program entry point
      static void Main(string[] args) {
         Student student1 = new Student("Alice", 20);
         student1.ShowDetails();
      }
   }
}

The output of the above code is −

Name: Alice, Age: 20

Key Components Summary

Component Purpose Required
Using statements Import namespaces and their classes Optional
Namespace Organize related classes Optional
Class Define data and methods Yes
Main method Program entry point Yes
Statements Execute program logic Yes
Comments Document code Optional

Conclusion

Every C# program consists of essential components: namespaces for organization, classes as containers, the Main method as the entry point, and statements for program logic. Understanding this structure is fundamental to writing well-organized C# applications.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements