Compare class syntax across programming languages: Typescript, C#, Dart

We’re often switching between programming languages for different projects. Most languages share the same concepts but differ in syntax or extra features that are (or are not) available. This is a go-to post for when we switch between languages and we need to refresh our knowledge.

For each of the languages, we’ll work out the same example that contains some basic language constructs, such as:

  • Interface
  • Enum
  • Class
  • Derived class (deriving from & implementing the previously mentioned class and interface)

I will also put a link at the end of this post to a Google Sheet that contains a summary of the differences.

Typescript

General info

Execute code online: https://www.typescriptlang.org/play/
Online docs: https://www.typescriptlang.org/docs/home.html

Typescript code example

Typescript syntax

Typescript remarks

You can specify access modifiers directly in the constructor what makes them automatically properties of the class. Example:

constructor(protected fuelType: FuelType)

When deriving: always call the parent constructor super() inside the brackets

constructor() {
super()
}

C# .NET

General info

Execute code online: https://dotnetfiddle.net/
Online docs: https://docs.microsoft.com/en-us/dotnet/csharp/

C# code example

C# syntax

C# remarks

The getter and setter for the same property are specified inside the same code block (whereas these are separate statements in most other languages):

DateTime buildDate {
get { return _buildDate; }
set { _buildDate = value; }
}

The constructor has the name of the class (not the word ‘constructor’) and when deriving, you call the parent constructor by putting base() before the brackets:

public Car() : base() { }

Dart

General info

Execute code online: https://dartpad.dartlang.org/
Online docs: https://dart.dev/guides/language/language-tour

Dart code example

Dart syntax

Dart remarks

Remarks about the constructor:

  • Optional parameters should be placed between brackets {}
  • If the constructor has no implementation, then use semi-column ‘;’ instead of brackets ‘{}’
  • call parent constructor with super(…)
Car(this.brand,{ this.hasGears = true }) : super(4);

Summary

This post is a work in progress and just compares exact the same code in different programming languages. I’m also working on a summary in a Google sheet:

Partner at Appdnd: agency specialized in App Design & Development. Interests: technology, startups, travel & watersports.

Partner at Appdnd: agency specialized in App Design & Development. Interests: technology, startups, travel & watersports.