Even if you don’t understand now, it’s okay. In the upcoming lessons, we will explain everything step by step. However, here we still need to explain in detail the general structure of a C# program. Please, everyone should have a preliminary impression.

1. Using Directives

Using directives are used to include namespaces that provide access to standard libraries and frameworks.

  using System;
  
  1. Namespace Declaration

A namespace is a container that organizes code elements and prevents naming conflicts.

  namespace MyNamespace
{
    // Classes, methods, variables, etc. go here
}
  
  1. Class Declaration

A class is a blueprint for creating objects that define data and behavior.

  class MyClass
{
    // Class members (fields, properties, methods, etc.)
}
  
  1. Main Method

The Main method is the entry point of a C# program where execution begins.

  class Program
{
    static void Main()
    {
        // Program logic
    }
}
  
  1. Variables and Methods

Inside the class, define variables (fields) and methods (functions) that perform tasks or provide functionality.

  class MyClass
{
    // Fields (variables)
    int myField;

    // Methods
    void MyMethod()
    {
        // Method body
    }
}
  
  1. Statements and Expressions

Statements are complete instructions that perform actions, while expressions compute values.

  int x = 10;        // Statement
int y = x + 5;     // Expression
  
  1. Control Structures

Control structures like if statements, loops (for, while), and switch statements control the flow of execution.

  if (condition)
{
    // Code block executed if condition is true
}
  
  1. Comments

Comments are used to explain code and make it easier to understand.

  // Single-line comment

/*
   Multi-line comment
*/
  
  1. Error Handling (Optional)

Error handling using try-catch-finally blocks to manage exceptions that may occur during program execution.

  try
{
    // Code that may throw exceptions
}
catch (Exception ex)
{
    // Handle exception
}
finally
{
    // Optional cleanup code
}
  
  1. Using External Libraries

Include external libraries and assemblies using using directives or by referencing them in the project file (csproj).

  using ExternalLibrary;