Primitives and strings
Kotlin primitives and strings appear as ordinary C# values. Nullable types keep their ?.
For this declaration in Mappings.kt:
Call it from C#:
Type mappings
Kotlin | C# |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
These mappings describe Kotlin APIs exported to C#. For consuming a C# library from Kotlin, see Types that cross the wire; nullable value types and the DateTimeOffset, TimeSpan, and Guid mappings are not supported in that direction.
Nullable values
Int? becomes int?, Boolean? becomes bool?, and String? becomes string?. Instant?, Duration?, and Uuid? become DateTimeOffset?, TimeSpan?, and Guid?. null stays distinct from 0, false, an empty string, or Guid.Empty.
A top-level function or property getter returning a nullable primitive, Instant?, or Duration? is evaluated twice when it returns a value. Keep those functions and getters free of side effects and ensure their result stays stable between evaluations. Ordinary instance methods evaluate once.
Char
Char maps to C# char, including non-ASCII characters. It works in properties, parameters, returns, and collections.
A standalone Char? is unsupported and can cause packaging to fail; nullable characters inside collections, such as List<Char?>, are supported. Use String for characters that need a surrogate pair; lone surrogate values do not reliably survive the conversion.
Instant
Use DateTimeOffset for Kotlin's kotlin.time.Instant:
log.FirstSeen represents the same instant as at, with its offset normalized to UTC. Use .UtcDateTime if you need a DateTime.
C# inputs preserve their full precision. Kotlin results lose fractions smaller than 100 ns, rounding down to the preceding tick.
Results must be within years 0001–9999. Out-of-range values, including
Instant.DISTANT_PASTandInstant.DISTANT_FUTURE, throwKotlinArgumentException.The older
kotlinx.datetime.Instanttype is not mapped.
Duration
Use TimeSpan for Kotlin's kotlin.time.Duration:
Kotlin results truncate toward zero to 100 ns precision.
All
TimeSpaninputs are accepted. Values beyond roughly ±146 years lose sub-millisecond precision in Kotlin, so a round trip may return a slightly different value.Infinite durations and finite values outside
TimeSpan's range (about ±10,675,199 days) throwKotlinArgumentException.
Uuid
Use Guid for Kotlin's kotlin.uuid.Uuid:
record.Id equals minted. All 128 bits are preserved, and Uuid.NIL maps to Guid.Empty.
Instant, Duration, and Uuid work as properties, constructor and method parameters, and function results. They are not supported as collection elements. For extension receiver support, see Extensions.
ByteArray
Use byte[] for Kotlin's ByteArray. A byte[] argument or result is always a fresh copy: nothing you do to it on either side reaches the other.
An empty ByteArray (byteArrayOf()) crosses as Array.Empty<byte>(), never null. ByteArray? becomes byte[]?, with null distinct from an empty array; payload.Checksum above is null for an empty payload. ByteArray works as a property, constructor and method parameter, and function result. It is not yet supported as a collection element (List<ByteArray>), as another array type (IntArray, Array<T>), or as an extension receiver.
C# names
Function and property names use PascalCase: string() becomes String(). For named arguments, use the generated parameter name. C# keywords are escaped, such as @abstract; reserved names such as error, value, and handle gain a trailing underscore (error_, value_, handle_). Positional calls are unaffected.