JS
C#

JavaScript to C#

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Async/Await4Array Methods → LINQ5Exception Handling6Collections7Generics8Delegates and Events9Records and Pattern Matching10File I/O
All Mirror Courses
JS
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 JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, 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  // : instead of extends
{
  public Dog(string name) : base(name) {}

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

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

Comparing to JavaScript

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

JS
JavaScript (What you know)
class Animal {
  #name;
  constructor(name) { this.#name = name; }
  get name() { return this.#name; }
  speak() { return this.#name + " makes a sound."; }
}

class Dog extends Animal {
  constructor(name) { super(name); }
  speak() { return this.name + " barks."; }
}

const d = new Dog("Rex");
console.log(d instanceof Animal); // true
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# uses : for inheritance; JS uses extends

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# auto-properties (get; set;) replace JS getter syntax

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# override keyword required; JS overrides silently

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# abstract keyword forces implementation in subclasses

Step-by-Step Breakdown

1. Inheritance Syntax

C# uses colon (:) for inheritance where JS uses extends. super() becomes base() in C#.

JS
JavaScript
class Dog extends Animal { constructor() { super(); } }
C#
C#
class Dog : Animal { public Dog() : base() {} }

2. Properties

C# auto-properties are cleaner than JS getters. { get; set; } auto-generates backing field and accessor.

JS
JavaScript
get name() { return this.#name; }
C#
C#
public string Name { get; private set; }

3. override Required

C# requires explicit override to replace a virtual/abstract method — prevents accidental hiding.

JS
JavaScript
speak() { return "barks"; } // silently overrides
C#
C#
public override string Speak() => Name + " barks.";

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • C# uses : for inheritance; JS uses extends
  • C# auto-properties (get; set;) replace JS getter syntax
  • C# override keyword required; JS overrides silently
Common Pitfall
Don't assume C# works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • extends → : (colon) in C#
  • super() → base() in C#
  • Auto-properties replace manual getter/setter
  • override keyword required for method overriding
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in C# to practice these concepts.
PreviousNext