Mastering C# for DDD Professionals
C# Syntax Basics
From Python to C#
Coming from Python, C# will feel both familiar and foreign. You'll recognize the same core logic—variables, loops, and functions—but the way you write them is stricter. The biggest changes are the punctuation and the type system.
First, C# uses curly braces {} to define code blocks, like the body of a loop or a function. Python uses indentation for this. Second, almost every statement in C# must end with a semicolon ;. This tells the compiler exactly where a statement ends. Forgetting it is a common first mistake.
Static Typing and Variables
The most significant shift from Python is C#'s static typing. In Python, you can write x = 10 and then x = "hello" without issue. Python figures out the type at runtime. C# is different. You must declare the type of a variable before you use it, and that type cannot change. This is called type safety.
This upfront type checking catches many potential bugs before your program even runs, which is a major advantage in large applications.
To declare a variable, you specify its type, give it a name, and optionally assign a value. Here are the most common types you'll use:
| Type | Description | Python Equivalent | Example |
|---|---|---|---|
int | Whole numbers | int | int age = 30; |
double | Floating-point numbers | float | double price = 19.99; |
string | Sequence of characters | str | string name = "Alice"; |
bool | True or false values | bool | bool isActive = true; |
char | A single character | (No direct equivalent) | char grade = 'A'; |
Notice that string is lowercase and char values use single quotes. Once you declare age as an int, you can't assign a string to it later. The compiler will stop you.
Controlling the Flow
Control flow in C# looks very similar to other C-style languages. The logic is the same as in Python, but the syntax uses parentheses () for conditions and curly braces {} for the code blocks.
// Conditional Statements
int temperature = 25;
if (temperature > 30) {
Console.WriteLine("It's hot outside.");
} else if (temperature < 10) {
Console.WriteLine("It's cold.");
} else {
Console.WriteLine("The weather is nice.");
}
For loops also follow a classic structure: initializer, condition, and iterator.
// A standard 'for' loop
for (int i = 0; i < 5; i++) {
Console.WriteLine("The count is: " + i);
}
// A 'while' loop
int countdown = 3;
while (countdown > 0) {
Console.WriteLine(countdown);
countdown--; // Same as countdown = countdown - 1
}
For iterating over collections like arrays or lists, C# has the foreach loop, which works just like Python's for item in list construct. It's clean and efficient for reading each element in a sequence.
string[] fruits = { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits) {
Console.WriteLine("I like to eat " + fruit);
}
Methods and The Entry Point
What Python calls functions, C# calls methods. When defining a method, you must specify the type of data it returns (its return type). If it doesn't return anything, you use the keyword void.
// This method takes two integers and returns an integer
int AddNumbers(int num1, int num2) {
return num1 + num2;
}
// This method takes a string and returns nothing (void)
void GreetUser(string name) {
Console.WriteLine("Hello, " + name + "!");
}
To call these methods, you use their name and provide the required arguments, just like in Python.
int sum = AddNumbers(5, 7); // sum will be 12
GreetUser("Bob"); // Prints "Hello, Bob!" to the console
Every C# console application needs an entry point, a special method where the program begins execution. This is the Main method. It is static, which is a concept we'll explore later, but for now, just know that it's the starting line for your code.
Here's a complete, basic C# program. It defines a class Program (don't worry about classes yet) and contains the Main method. Inside Main, it uses Console.WriteLine() to print text and Console.ReadLine() to get user input.
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("What is your name?");
string userName = Console.ReadLine();
Console.WriteLine("Hello, " + userName + "!");
}
}
How does C# define a code block, such as the body of an if statement or a for loop?
In C#, almost every statement must end with a semicolon ;.
This covers the essential syntax. You've seen how C#'s structure, type system, and core constructs differ from Python. With these fundamentals, you're ready to start writing simple C# applications.
