| Type checks | Checked at compile time. Every variable has a type (int, String, or var for locals). | Decided at run time. Variables are name tags; hints such as x: int are for checkers. | Decided at run time. Types on parameters, returns and properties are checked, and declare(strict_types=1) stops automatic conversion. | Checked at compile time. Locals can use var, and types also say whether null is allowed (string?). | Checked at compile time. Types are inferred (let x = 3), and values that may be nil have their own type, such as String?. |
|---|
| Making a variable | Declared with a type: int count = 0;. | Created on first assignment: count = 0. | Created on first assignment with a $: $count = 0;. | int count = 0; or var count = 0; | let count = 0 (constant) · var count = 0 (variable) |
|---|
| Code blocks | Braces { }, with ; ending each statement. | Indentation, usually four spaces. | Braces { } and ; at the end of statements. | Braces { } and ; · braces usually open on their own line. | Braces { } · no ; at line ends and no parentheses around if conditions. |
|---|
| Lists and maps | List, Map and Set interfaces with ArrayList and HashMap; arrays are separate. | list, dict, set and tuple are built in. | One array type serves as list and map, and it is copied by value. | List<T>, Dictionary<K,V>, HashSet<T> and arrays T[] · [1, 2, 3] collection expressions since C# 12. | Arrays [Int], dictionaries [String: Int], Set<Int> · all value types, copied when passed. |
|---|
| Defining a class | class Book { } · one public class per file, named like the file. | class Plant: · one file may hold many classes. | class Plant { } · usually one class per file (PSR-4). | class Book { } · file and class names may differ. Use record for value-only types. | Reference type class Book { } and value type struct Book { } · structs usually come first. |
|---|
| Constructor | A method named after the class; several allowed with different parameters (overloading). | One def __init__(self, ...); extra constructors use @classmethod. | One __construct(); public on a parameter declares the property too (8.0). | A method named after the class · since C# 12 also a primary constructor, class Book(string title). | init(title: String) · structs get a memberwise init for free. |
|---|
| The current object | this, never written as a parameter. | self, written out as the first parameter. | $this-> (in static code, self:: or static::). | this, and base for the parent. | self, and super for the parent. |
|---|
| Hiding data | public, protected, package-private and private, enforced by the compiler. | No keywords. _name is an agreement, __name is renamed. | public, protected, private · readonly (8.1) · 8.4 private(set). | Six levels including public, private, protected and internal · properties such as { get; private set; }. | open, public, internal (default), fileprivate, private · private(set) closes only writing. |
|---|
| Inheritance | class Dog extends Animal · one parent class only. | class Truck(Vehicle): · several parents allowed. | class Truck extends Vehicle · one parent · traits add shared code. | class Manager : Employee · one parent class · overriding needs virtual and override. | class Run: Workout · classes only · override is required, final stops it. |
|---|
| Abstract classes and contracts | abstract class and interface (a class may implement many). | ABC and @abstractmethod from abc, or typing.Protocol. | abstract class and interface (implement many). | abstract class and interface (several allowed, default methods possible). | protocol plus extension defaults · structs and enums conform too. No abstract classes. |
|---|
| Class-level values and methods | static fields and methods. | Variables in the class body · @staticmethod · @classmethod. | static properties and methods · self::$n · late static binding with static::. | static members and static class, which cannot be instantiated. | static let and static func · class func when subclasses may override. |
|---|
| No value | null | None | null · nullable types look like ?string. | null, handled with ?. and ??. | nil and optionals T? · unwrap with if let, guard let, ?. and ??. |
|---|
| Exceptions | try / catch / finally · checked exceptions must be declared with throws. | try / except / finally · no declared exception lists. | try / catch / finally · exceptions (Exception) and engine errors (Error) are separate. | try / catch / finally · unlike Java, methods do not declare what they throw. | throws, try, do / catch · throwing functions are marked throws. |
|---|
| Build and package tools | Maven (pom.xml) or Gradle (build.gradle). | pyproject.toml with pip (in a venv) or uv. | Composer (composer.json, PSR-4 autoloading, Packagist). | The dotnet CLI · .csproj projects in a .sln · packages from NuGet. | Swift Package Manager (Package.swift, swift build) · apps in Xcode. |
|---|
| How code runs | javac compiles to bytecode, then the JVM runs java Main. | python3 main.py · the interpreter runs the source directly. | php main.php, or a web server (PHP-FPM) that runs the script fresh for each request. | dotnet build compiles to IL and the .NET runtime runs it (dotnet run). | swiftc compiles to machine code (swift run). Xcode builds apps and runs them on a device or simulator. |
|---|