Press "Enter" to skip to content

Comparing Kotlin to C#

vpandzic 0

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.

kotlin-vs-c-sharp 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:

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

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:

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.

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:

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

Functions

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.

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:

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)

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

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:

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.

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

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

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

 

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.

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

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”.

 

Loops

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

In C# this would look like following:

 

    Leave a Reply

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