This Tutorial Describes the Syntax and Structure of C# Programs. In this Tutorial, You Will Learn About the Different Elements of a C# Program:
The syntax of C# describes the rules that you must follow when writing your C# programs. It includes declaration of variables, definition of classes and methods, usage of data types, usage of braces and semicolons, etc. Regardless of whether you are new to the syntax of C# or not, having knowledge about these rules will be of some help.
In this tutorial, we will describe C# syntax using examples, C# program structure, and errors to avoid while using C#.
=> Read Through The C# Guide For Beginners Here

Let’s again have a look at the previous program that we wrote. We wrote this program to print out a simple text in the console i.e. “Hello World”. But if you look closely at the program there are several other components in the program that make it.
Keep this program in mind as we are going to take this program as a reference for the upcoming topics.

If we look at the above program we will find that the above program consists of several different components.
C# Program Structure: C# Syntax and Basic Rules
C# is a modern, object-oriented language developed by Microsoft. It is strongly typed, block-scoped, and designed to run on the .NET platform.
- Case Sensitivity: C# distinguishes between uppercase and lowercase letters. myVariable and MyVariable are two completely different identifiers.
- Statements End with Semicolons: Every individual execution statement must terminate with a semicolon ;.
- Block Enclosure with Curly Braces: Blocks of code (namespaces, classes, methods, conditionals, loops) are enclosed within { }.
Naming Conventions:
- PascalCase: Used for classes, methods, and properties (e.g., Program, Console, Main).
- camelCase: Used for local variables and parameters (e.g., totalAmount, userName).
- Comments:
- Single-line: // This is a comment
- Multi-line: /* This is a multi-line comment */
- XML Documentation: /// <summary>Description</summary>
Basic Program Structure
A typical C# file consists of namespace imports, a declared namespace, a class definition, and an entry point method.
C#
using System; // 1. Namespace Import
namespace HelloWorldApp // 2. Namespace Declaration
{
class Program // 3. Class Definition
{
static void Main(string[] args) // 4. Entry Point Method
{
// 5. Code Statement
Console.WriteLine("Hello, World!");
}
}
}
Key Components
| Component | Description |
| Namespace | Organizes related classes and other types to prevent naming conflicts across projects. |
| Class | Defines a type that can contain methods, fields, properties, constructors, and other members. |
| Main() Method | The traditional entry point where execution begins in a C# program. Modern C# programs can also use top-level statements without explicitly declaring Main(). |
| Methods Inside the Class | Define reusable blocks of code that perform specific tasks and can accept parameters or return values. |
| Class Definition or Class Members | Includes fields, properties, constructors, and other members that define an object’s data and behavior. |
| Statements | Instructions executed by the program, such as variable declarations, method calls, assignments, and conditional statements. Many statements end with a semicolon (;). |
| Comments | Notes added to explain code. Single-line comments begin with //, while multiline comments are enclosed in /* and */. |
| using System; | Imports the System namespace so types such as Console can be referenced without writing their fully qualified names. |
| Console.WriteLine() | A method that writes text or other values to the console and moves to a new line. |
As discussed earlier, the execution of the C# program starts from the Main method, hence the program should have the Main method as its starting point.
Before we begin with an explanation of the C# components lets recall a few points:
- A program needs to have a reference to the .Net framework that has been used in the program. For our current program to reference.Net, we use “using”. For Example “using System” in our above program.
- After defining the references we declare a namespace. Syntax Example- namespace ConsoleApplication.
- Then we declared a class using a class keyword. Syntax Example- class Program.
- Inside the class, we have a Main method. This is the starting point of the execution.
- Inside the Main method, we have defined “Console” which is a class in the .Net framework. The “WriteLine()” is a method that is used to display a message in a console.
- Similar to “WriteLine()”, “ReadLine()” is also a method that reads user input.
In the above program, we wrote a text in the console application and discussed various parts of the program. Here, we will discuss various parts that were used in conjuncture with each other to make-up a valid syntax in detail. We will discuss components, their definition and ways in which we have to declare them in the program.
Using Keyword
The using keyword is used to handle any object that is being used to implement the namespace. It is used to import a namespace. As a namespace is a collection of classes and each class has its own functionality, so using the keyword provides us with the facility to implement different functionality from the namespace that has been imported.
C#
// 'using', 'namespace', 'class', 'static', and 'void' are all C# keywords
using System;
namespace MyApp
{
class Program
{
static void Main()
{
// 'int' is a keyword for integer data type; 'age' is a user-defined identifier
int age = 25;
// 'if' is a control flow keyword
if (age >= 18)
{
Console.WriteLine("Adult");
}
}
}
}
Namespace
The namespace is a collection of related classes and objects. It is designed to offer a way to keep a different set of objects segregated from each other. This allows the programmers to declare one class inside a namespace and another class inside another namespace without causing a conflict.
Let’s say we are creating two namespaces namely “fruits” and “colors”. Both these namespaces can have a class called “Orange” without creating any conflict with each other. A namespace is defined by writing your namespace name preceded by keyword namespace.
For Example:
C#
namespace Fruits
{
public class Orange
{
public string Color => "Bright Orange";
}
}
namespace Colors
{
public class Orange
{
public string HexCode => "#FFA500";
}
}
class Program
{
static void Main()
{
// Fully qualified names prevent any confusion for the compiler
Fruits.Orange fruit = new Fruits.Orange();
Colors.Orange shade = new Colors.Orange();
Console.WriteLine(fruit.Color); // Output: Bright Orange
Console.WriteLine(shade.HexCode); // Output: #FFA500
}
}
Class
Defining a class is similar to defining a blueprint of a data type. Class doesn’t actually define the data but they arrange the data as a meaningful entity.
For Example, if we have a class called “Fruit”, then we can specify different related objects inside the class such as mango, apple, grapes, etc. Thus, the class fruit will contain different fruit objects with different properties such as color, taste, smell, edible, etc. Each of these objects and property will be a part of the class.
Similar to the above example, the object-oriented programming language such as C# also defines different properties inside i.e. fields, methods, conditions, events, etc. The objects inside the class contain a definition of the operations that can be performed.
Objects are basically an instance of the class and the methods or the variables that are the members of the class. Thus, in short, the class allows you to create your own custom objects by grouping other methods, events and object types.
A class is defined by putting the keyword “class” before the name of the class and the class body should be defined by a pair of curly braces.
C#
using System;
namespace MyApp
{
// Class declaration: A blueprint for a Car object
public class Car
{
// Field (Data state)
public string Model;
// Method (Behavior / Logic)
public void Drive()
{
Console.WriteLine($"{Model} is driving.");
}
}
class Program
{
static void Main()
{
// Creating an object (instance) from the Car class blueprint
Car myCar = new Car();
myCar.Model = "Tesla Model 3";
myCar.Drive(); // Output: Tesla Model 3 is driving.
}
}
}
Access Modifiers
Access modifiers define the accessibility of an object and its components. All the C# components have their own access level that can be controlled by defining the scope of accessibility for the member objects inside the class by using access modifiers.
To define the accessibility level of the object we have to declare it by using one of the keywords provided by C# language i.e. Public, Private, Protected and Internal. Access modifier is declared by using either of the keywords mentioned above before the class or a method.
For Example:
C#
using System;
namespace MyApp
{
public class BankAccount
{
// Private field: accessible only from inside the BankAccount class
private decimal balance = 1000.00m;
// Public method: accessible from anywhere in the program
public void ShowBalance()
{
Console.WriteLine($"Current Balance: ${balance}");
}
}
class Program
{
static void Main()
{
BankAccount account = new BankAccount();
account.ShowBalance(); // Allowed: ShowBalance is public
// Compiler Error: balance is private and inaccessible here
// Console.WriteLine(account.balance);
}
}
}
Method
A method is a group of statements that execute together to perform a certain task. Every C# program will contain at least one class with at least one Main method.
A method contains the following definition. It starts with an access modifier declaration, then a return type and later we define the method name and inside the round bracket, we have the parameters defined.
C#
using System;
namespace MyApp
{
class Program
{
// A method that takes two integers, adds them, and returns the result
public static int AddNumbers(int a, int b)
{
return a + b;
}
static void Main()
{
// Calling the method and storing the returned result
int sum = AddNumbers(10, 15);
Console.WriteLine($"The sum is: {sum}"); // Output: The sum is: 25
}
}
}
After the method has been defined, a set of commands or statements can be written within the curly braces. Arguments can be passed within the round bracket just after the method name.
These arguments are useful while calling a method with certain parameters. In the above example, we have passed only one parameter that is “args” with the argument type as an array of string.
Constructor
Constructors are the special methods in a class that is automatically invoked whenever an instance of the particular class is created. The main advantage of the constructor is that it can initialize the private fields of a class. A class can have a number of constructors and it doesn’t need to have any return type.
Only one static constructor is allowed inside a class and that cannot be parameterized. Constructors are declared by using any access modifier and then by using the class name in which it is created.
For Example, a constructor definition will look something like this.
C#
using System;
namespace MyApp
{
public class Car
{
public string Model;
public int Year;
// Constructor: automatically runs when 'new Car(...)' is called
public Car(string model, int year)
{
Model = model;
Year = year;
}
public void DisplayInfo()
{
Console.WriteLine($"Car: {Year} {Model}");
}
}
Data Types
Data types are used in almost all the programming languages. A data type tells the compiler what kind of value a variable will hold. C# has several built-in data types such as String, Integer, Float, Boolean, etc. Each data type has its own set of definitions for the values it can hold.
A data type is used to declare a variable by the preceding variable with the data type keyword.
C#
using System;
namespace MyApp
{
class Program
{
static void Main()
{
// Value types (store values directly)
int score = 95;
double temperature = 98.6;
bool isPassed = true;
char grade = 'A';
// Reference type (stores a reference to the data)
string playerName = "Alex";
Console.WriteLine($"Player: {playerName} (Grade: {grade})");
Console.WriteLine($"Score: {score}, Temp: {temperature}, Passed: {isPassed}");
}
}
}
We will discuss more on data types in our upcoming tutorials.
Curly Braces
We saw the extensive usage of curly braces “{}” in our previous programs and declarations which is why it is referred to as one of the most important parts of any program. It defines the beginning and the end of any logical statement in a program block.
The curly braces are not only restricted to the C# language, but they are also found in several other languages like Java, C++, etc. You can clearly see our first “Hello World” code that they are used to wrap multiple lines of code together. It clearly marks the starting and the endpoint of the logical statement for the compiler.
Any logical entity like Namespace, Class, Method, Conditional statement, Loops, etc. should be enclosed inside braces to clearly mark the start and end of the statement for the compiler.
C#
using System;
namespace MyApp // Outer block: Namespace
{
class Program // Class block
{
static void Main() // Method block
{
int outerValue = 10;
if (outerValue > 5) // Control flow block
{
int innerValue = 20; // Scoped strictly to this if-block
Console.WriteLine(outerValue + innerValue); // Works: 30
}
// Compiler Error: innerValue does not exist outside its curly braces block
// Console.WriteLine(innerValue);
}
}
}
Conclusion
In this tutorial, we learned the usage and declaration of various components of the C# program. We gathered some high-level understanding of the concepts. We learned how to declare various components and usage of various components like classes and methods.
We also discussed the importance of using curly braces and learned a few things about constructor. In our upcoming tutorials, we will discuss all these components that make up your program in detail and see how to use them to implement different scenarios.
Take Away
- A namespace is a collection of one or more classes and is designed to segregate different sets of objects/classes.
- Class is a collection of methods, statements, events, properties, etc. of similar type.
- Access modifiers are used to define the accessibility level of a class or method.
- Constructors are special methods with the same name as the class and are invoked as soon as an instance of the class is created.
=> Explore The Entire Series Of C# Training Tutorials Here





