PY
C#

Python to C#

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Collections & LINQ4Async/Await5Exception Handling6File I/O7Generics8Delegates and Events9Records and Pattern Matching10Interfaces
All Mirror Courses
PY
C#
Variables & Types
MirrorLesson 1 of 10
Lesson 1

Variables & Types

Static typing vs dynamic typing

Introduction

In this lesson, you'll learn about variables & types in C#. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
PY
From Python:

In Python, you're familiar with static typing vs dynamic typing.

C#
In C#:

C# has its own approach to static typing vs dynamic typing, which we'll explore step by step.

The C# Way

Let's see how C# handles this concept. Here's a typical example:

C#
C# Example
string name = "Alice";
int age = 30;
double score = 9.5;
bool active = true;

const int Max = 100; // compile-time constant

// var — type inference
var city = "Istanbul"; // inferred as string

// Types cannot change
int x = 42;
// x = "string"; // compile error

// Nullable
int? maybeAge = null;
string? maybeName = null;

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
name = "Alice"
age = 30
score = 9.5
active = True

MAX = 100  # constant by convention

# Type hints (optional)
city: str = "Istanbul"

# Variables can change type
x = 42
x = "now a string"  # valid
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# types are fixed at declaration; Python types are dynamic

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# var infers type (still fixed); Python variables can change type

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# const is compile-time; Python UPPER_CASE is just convention

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# nullable types (int?) explicitly allow null; Python uses None freely

Step-by-Step Breakdown

1. Explicit Types

C# requires a type for every variable. The compiler verifies assignments at compile time — no runtime type errors.

PY
Python
age = 30
C#
C#
int age = 30;

2. var Inference

C#'s var infers the type from the initializer — like Python's dynamic typing but the type is still fixed after declaration.

C#
C#
var name = "Alice";   // string, immutable type
var count = 0;        // int, immutable type

3. Nullable Types

C# distinguishes between int (never null) and int? (can be null). Python's None can be assigned to any variable.

PY
Python
age = None  # any variable can be None
C#
C#
int? age = null;  // explicitly nullable
int required = 30; // cannot be null

Common Mistakes

When coming from Python, developers often make these mistakes:

  • C# types are fixed at declaration; Python types are dynamic
  • C# var infers type (still fixed); Python variables can change type
  • C# const is compile-time; Python UPPER_CASE is just convention
Common Pitfall
Don't assume C# works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • C# types fixed at declaration; Python dynamic
  • var infers type but keeps it fixed
  • int? for nullable; int for non-nullable
  • C# const is compile-time; Python UPPER_CASE is convention
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in C# to practice these concepts.
OverviewNext