DEV Community

Cover image for C# to Python Reference Guide
Abdul
Abdul

Posted on

C# to Python Reference Guide

Coming from a solid C# background, jumping into Python can feel like a bit of a culture shock. My brain is constantly looking for semicolons, trying to type public static void Main, or reaching for this instead of self.

To save myself from constantly googling the exact syntax differences every single day, I decided to build a quick reference cheat sheet. It breaks down the core concepts side by side, comparing how we do things in C# versus how Python handles them. Whether you are moving into AI, web development, or you want to do a total stack switch, hope this helps 🙌🏻 you got this!

C# to Python Reference Guide

1. General Architecture & Syntax

Feature C# Python
Typing Static (compile-time checked) Dynamic (runtime checked, typing hints optional)
Execution Compiled to IL, run by CLR Interpreted to bytecode, run by PVM
Blocks Curly braces { } Strict indentation (whitespace)
Line Endings Semicolons required ; Newline (no semicolons)
Entry Point static void Main(string[] args) if __name__ == "__main__":
Variables int x = 5; or var x = 5; x = 5 (No keywords needed)
Constants const int MAX = 10; MAX = 10 (Convention only)
Comments // Single, /* Multi */ # Single, """ Multi / Docstrings """

2. Object-Oriented Programming (OOP)

Feature C# Python
Class public class Car { } class Car:
Instantiation var car = new Car(); car = Car() (No new keyword)
Constructor public Car(string name) def __init__(self, name):
Instance Ref. this (Implicit) self (Explicitly passed as first param)
Access Mods public, private, protected All public. Prefix _ for internal convention.
Properties public string Name { get; set; } Direct attributes, or @property decorator
Inheritance class Car : Vehicle (Single) class Car(Vehicle): (Multiple allowed)
Interfaces interface IVehicle { ... } "Duck Typing", abc.ABC, or typing.Protocol

3. Functions & Methods

Feature C# Python
Naming Norm PascalCase() snake_case()
Overloading Supported natively Not supported (use default args or **kwargs)
Void Return void MyMethod() Returns None by default
Var Args params int[] numbers *args (positional), **kwargs (named)
Lambdas x => x * 2 lambda x: x * 2
Generators yield return item; yield item

4. Data Types & Control Flow

Feature C# Python
Null/Empty null None
Booleans true, false True, False (Capitalized)
Logical Ops &&, ||, ! and, or, not
Ternary cond ? true : false true_val if cond else false_val
Switch switch(x) { case 1: ... } match x: case 1: ... (Python 3.10+)
Arrays/Lists int[], List<int> list [1, 2, 3] (Dynamic, mixed types)
Dictionaries Dictionary<string, int> dict {"key": 1}
Strings $"Hello {name}" f"Hello {name}"
Type Check if (obj is MyClass) if isinstance(obj, MyClass):

5. Error Handling & Concurrency

Feature C# Python
Exceptions try { } catch(Exception e) { } try: ... except Exception as e: ...
Throwing throw new Exception(); raise Exception()
Async async / await (Task) async / await (asyncio coroutine)
Disposal using (var f = File.Open()) with open(...) as f: (Context Managers)

Top comments (0)