C# is a modern, object-oriented programming language designed for clarity, simplicity, and power. Before diving into complex applications, it’s essential to understand the basic syntax and structure that form the foundation of every C# program.
From defining variables and writing methods to using control flow and organizing code into classes, learning C# syntax helps you write clean, readable, and maintainable code. In this post, we’ll explore the building blocks of C#, giving you a solid starting point for writing your first programs with confidence.
🧱 The Structure of a Basic C# Program
Here’s a simple C# program:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
}
}
}
Let’s break that down:
✅ using System;
This line imports the System namespace, giving access to built-in classes like Console
.
✅ namespace HelloWorld
Defines a container (namespace) to logically group your code. You can have multiple classes inside a namespace.
✅ class Program
This defines a class called Program
. In C#, all code must be inside a class or struct.
✅ static void Main(string[] args)
This is the entry point of the program. It’s where the application begins execution.
📌 Key Concepts in C# Syntax
1. Statements and Semicolons
Every instruction (called a statement) ends with a semicolon (;
):
int age = 30;
2. Variables and Data Types
C# is statically typed, so you must declare a variable with a type:
string name = "Alice";
int score = 100;
double temperature = 36.6;
bool isActive = true;
3. Comments
Comments help explain your code:
// This is a single-line comment
/*
This is a
multi-line comment
*/
🔁 Control Flow Examples
if/else
int age = 18;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are underage.");
}
Loops
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Loop count: " + i);
}
🧱 Methods (Functions)
Methods help organize and reuse code:
static void Greet(string name)
{
Console.WriteLine("Hello, " + name + "!");
}
Call the method like this:
Greet("Alice");
🎯 Object-Oriented Basics
C# is object-oriented, so you’ll often create classes and objects:
class Person
{
public string Name;
public void SayHello()
{
Console.WriteLine("Hello, my name is " + Name);
}
}
And use it like this:
Person p = new Person();
p.Name = "Bob";
p.SayHello();
🧪 Try It Yourself
Open Visual Studio or use the CLI:
dotnet new console -n SyntaxDemo
cd SyntaxDemo
dotnet run
Replace the code in Program.cs
with any of the samples above and run it.
📘 Final Thoughts
Mastering C# syntax and structure is the first big step toward becoming a capable developer. As you progress, these foundations will help you write better code, build powerful applications, and understand advanced topics like classes, interfaces, and LINQ.
If you’re ready to take your learning further, check out our C# Essentials and Object-Oriented Programming courses at Ocean Stack — or contact us for more details.
Happy coding! 👨💻👩💻