Member-only story

Complete C# to Kotlin Syntax Comparisons

Kotlin Cheat Sheet for C# developers

Vincent Tsen
8 min readOct 16, 2021

Introduction

If you are a C# developer and new to Kotlin, this article is for you. It gives you a quick overview of syntax comparisons between C# and Kotlin. It also can be served as your quick reference guide for Kotlin syntax.

Please note that the standard naming conventions and coding styles for these 2 languages are also different. You can see the differences in the following code examples.

If you have difficulty understanding the Kotlin syntax, please refer to the examples from kotlinlang.org.

Methods vs Functions

C#

public virtual void PrintMessageWithPrefix( 
String message,
String prefix = “Info”)
{
Console.WriteLine($”{prefix} {message}”);
}

Kotlin

fun printMessageWithPrefix(  
message: String,
prefix: String = "Info") {
println("[$prefix] $message")
}

If the visibility modifier is not stated in Kotlin, it is public and virtual by default.

Variables

C#

string a = "initial";  
const int b = 1;
const int c = 3;

--

--

No responses yet