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#
Classes & OOP
MirrorLesson 2 of 10
Lesson 2

Classes & OOP

Object-oriented programming

Introduction

In this lesson, you'll learn about classes & oop 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 object-oriented programming.

C#
In C#:

C# has its own approach to object-oriented programming, 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
public abstract class Animal {
    public string Name { get; }  // auto-property

    protected Animal(string name) { Name = name; }

    public abstract string Speak();
}

public class Dog : Animal {
    public Dog(string name) : base(name) {}

    public override string Speak() =>
        Name + " barks.";
}

var d = new Dog("Rex");
Console.WriteLine(d.Speak());
Console.WriteLine(d is Animal); // true

Comparing to Python

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

PY
Python (What you know)
class Animal:
    def __init__(self, name: str):
        self._name = name  # convention: _ = "private"

    @property
    def name(self) -> str:
        return self._name

    def speak(self) -> str:
        return f"{self._name} makes a sound."

class Dog(Animal):
    def speak(self) -> str:
        return f"{self.name} barks."

d = Dog("Rex")
print(d.speak())
print(isinstance(d, Animal))
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# uses : for inheritance; Python uses parentheses class Dog(Animal)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# auto-properties replace Python's @property decorator

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# abstract forces override; Python has no abstract keyword by default

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# override required; Python overrides silently

Step-by-Step Breakdown

1. Inheritance Syntax

C# uses colon for both inheritance and interface implementation. super() becomes base() in the constructor.

PY
Python
class Dog(Animal):
    def __init__(self, name):
        super().__init__(name)
C#
C#
class Dog : Animal {
    public Dog(string name) : base(name) {}
}

2. Properties

C# auto-properties replace Python's @property. { get; } creates a read-only property; { get; set; } is read-write.

PY
Python
@property
def name(self): return self._name
C#
C#
public string Name { get; private set; }

3. Abstract Classes

C# abstract enforces that subclasses implement the method — Python uses NotImplementedError by convention.

PY
Python
def speak(self): raise NotImplementedError
C#
C#
public abstract string Speak(); // must override

Common Mistakes

When coming from Python, developers often make these mistakes:

  • C# uses : for inheritance; Python uses parentheses class Dog(Animal)
  • C# auto-properties replace Python's @property decorator
  • C# abstract forces override; Python has no abstract keyword by default
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

  • class Dog(Animal) → class Dog : Animal
  • super() → base()
  • @property → auto-property { get; set; }
  • abstract keyword enforces method implementation
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in C# to practice these concepts.
PreviousNext