Comparing Kotlin to C#

One of the best way for learning new language is to compare it with language you already know. In this article we will make comparison between Kotlin and C# languages. Kotlin is now official language for Android development. In this part I will not cover all language differences. Comparing Kotlin to C# would probably require few more posts. Rather, I will explain some random language features and differences I learned first.

comparing kotlin to c#
comparing kotlin to c#


Creating new object

In C# most common way of creating new instance of the class is to use new keyword like this:

Animal animal=new Animal();

In Koltin we don’t use new keyword. We just write:

Animal animal = Animal()

Notice, btw that Kotlin does not require semicolons too, and creating of new object is just like in Swift. We don’t use new keyword and because of that someone will say that it makes harder to distinguish between method call and creating of new object. I disagree because most IDE-s will make Animal() different color then some method call like this getSomething() . Also, convention in Kotlin is to make method calls camel cased which is another difference.

Creating variables

Creating variables can sometimes seem similar because this is both valid in C# and Kotlin:

var someText="Some string";

Kotlin and C# can both inferr type of variable. However when we want to explicitly say what type of variable is in C# we will first say what type of variable is than it’s name whereas in Kotlin we will first say what is name of variable and then what type of variable is. Later seems much more like we will say in English. Declare variable x which is of type String.

 var a:String //Kotlin
String a; //C#

So if you know Swift or TypeScript this Kotlin way of declaring variables will be similar to you. One important thing, is that var a:String won’t compile in Kotlin if we don’t initalize variable a . In C# String is by default null , int is 0, and all reference types are null . Kotlin tries, completely similar to Swift, to be more safe language so it won’t allow declaring variable of type String (or other type) without initializing it. It won’t allow to assign null to it. It will cause compile error. So, to make declaration above to compile we will have to initiliaze variable a like this:

var a: String="5"

What if want that variable sometimes can be null? We will have to explicitily say that like in example below:

var a: String?


Once again, just like declaring variable feels more natural in Kotlin,  function definition reads better in Kotlin in my opinion.

We have function sumNumbers which accepts two integers and returns sum of those numbers.

fun sumNumbers(a:Int, b:Int):Int {
   return a+b;

In Kotlin, just like in JavaScript or Swift we have special fun  keyword for functions (function in TypeScript, func in Swift). Function parameters are declared just like explained before, and return type comes last.

Both C# and Koltin can have default values for function parameters:

fun sumNumbers(a:Int, b:Int=7):Int { return a+b; }
int sumNumbers(int a,int b=7){return a+b;}

One important thing, and difference between C# function parameters and Kotlin function parameters is that Kotlin’s are immutable meaning that changing and assing new values inside functions is not allowed (once again- similar to Swift)

fun sumNumbers(a:Int,b:Int){
   return a+b;


We could create new variable, with different name if we want, but changing existing one will result in compile error.

fun sumNumbers(a:Int,b:Int){ 
  var newA=someOtherFunction(a);
   return newA+b; 

That bring as to next topic immutability.

Imuttability in Kotlin

Kotlin does not only have var keyword, but also val keyword. But, val doesn’t mean imuttable- it means it’s value can’t change but it doesn’t mean that is always the same. There is another blog post explaining that val doesn’t mean imuttable, it just means readonly. Consider this example:

val a=Random().nextInt()

Compiler won’t allow reassigning value for variable a. However, variable can be different each time, which depends on the result of nextInt()  function.

Extension methods

One thing I like very much in C# is extension methods. They allow to extend functionallity of some type as if they always existed in language.

public static char FirstLetterUppercased(this string value)
// Uppercase the first letter in the string.
if (value.Length > 0)
char[] array = value.ToCharArray();
array[0] = char.ToUpper(array[0]);
return array[0]
return '';

fun String.firstLetterUppercased():Char{

   if (this.Length > 0) {
     val array = this.toCharArray()
     array[0] = char.toUppeCase(array[0]);
     return array[0]
  return '';


In C#, there is much more ceremony when creating extension method. Extension method in C# is static method that must live inside static class. Also, first parameter must have this  prefix. in Kotlin it is much simpler. You get  magic this keyword for free and it reffers to current String variable  which “consumes” this firstLetterUppercased  method. Also, there is no static method inside static class thing. You just prefix method with type you are extending: String.firstCharToUpper. This is by far shortest syntax for creating extension methods in any language.


Properties in Kotlin and C# have different  syntax.  In C# we write getters and setters inside curly braces. Get doesn’t have parentheses, and set can have because it receives imlicitly newValue

private string name;
public string Name{

     return name;

public string Name{get;set;}

In Kotlin we write setters and getter immediately after property name (or property type if you want to explicity provide it)


var _n;
var name:string get(){
   return _n;

Notice how even get have parantheses even it can’t receive any parameters.

Classes and inheritance

Classes in Kotlin and C# are created in the same way using class keyword.

class Developer{


C# constructor has same name as class, can have parameters but it doesn’t need to, and ofcourse doesn’t have return type.

public class Developer{

  Developer(string price){


A class in Kotlin can have primary constructor and one or more secondary constructors. The primary constructor is part of header and goes immediattely after class name. Furthermore , primary constructor can’t have any code. Because of that any initialization logic must be put inside init method.  Classes can also have secondary constructor(s), and it is called just “constructor”.

class Developer (developerName:string){

  var name:String;
  var projectsCount:String;
  constructor(yearsOfExperience:Int, numberOfSuccessfullyProjects:Int){



For iterates through anything that provides an iterator. This is equivalent to the foreach loop in languages like C#. The syntax is as follows:

for (item: Int in listOfNumbers) {
// ...

In C# this would look like following:

foreach(var item in listOfNumbers) {

// ...



3 thoughts on “Comparing Kotlin to C#

Leave a Reply

Your email address will not be published. Required fields are marked *.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>