Kotlin Extension Methods

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • fun TypeName.extensionName(params, ...) { /* body */ } // Declaration
  • fun <T: Any> TypeNameWithGenerics<T>.extensionName(params, ...) { /* body */ } // Declaration with Generics
  • myObj.extensionName(args, ...) // invocation

Remarks

Extensions are resolved statically. This means that the extension method to be used is determined by the reference-type of the variable you are accessing; it doesn't matter what the variable's type is at runtime, the same extension method will always be called. This is because declaring an extension method doesn't actually add a member to the receiver type.



Got any Kotlin Question?