# Is there C# unsafe code like in kotlin?

**URL:** https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138
**Category:** Language Design
**Created:** [July 18, 2026, 11:34am UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138 "2026-07-18T11:34:57Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![MIha\_Markic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/miha_markic/32/11397_2.png) [@MIha\_Markic](https://discuss.kotlinlang.org/u/MIha_Markic)
#### Post date: [July 18, 2026, 11:34am UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/1 "2026-07-18T11:34:57Z")

</div>

C# has an _[unsafe](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code)_[keyword](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code) which allow for more dangerous and low-level access to data. Is there something similar in kotlin? I couldn’t find anything related so far.

---

<div class="post-metadata">

### Author: ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)
#### Post date: [July 18, 2026, 12:08pm UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/2 "2026-07-18T12:08:59Z")

</div>

The JVM is designed as a safe, managed platform; I think the only way to do potentially unsafe things in it is to break out of Java into native code using [Java Native Interface (JNI)](https://docs.oracle.com/javase/8/docs/technotes/guides/jni/). (This is of course highly platform-specific and potentially dangerous.)

So the same applies to Kotlin/JVM.

(I don’t know whether Kotlin/JS or Kotlin/Native provide any other relevant features. But of course they won’t be available in Kotlin Multiplatform.)

What unsafe things do you want to do?

---

<div class="post-metadata">

### Author: ![MIha\_Markic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/miha_markic/32/11397_2.png) [@MIha\_Markic](https://discuss.kotlinlang.org/u/MIha_Markic)
#### Post date: [July 18, 2026, 2:18pm UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/3 "2026-07-18T14:18:50Z")

</div>

Hey, thanks for the answer, I thought that might be the case. Actually I’m just trying to compare the two languages as I’m thinking to host a presentation of kotlin for c# devs.

---

<div class="post-metadata">

### Author: ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)
#### Post date: [July 19, 2026, 3:31am UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/4 "2026-07-19T03:31:34Z")

</div>

AFAIK Kotlin itself doesn’t have any “unsafe” operations, but the JVM does. There was another thread here where somebody mentioned the `Unsafe` Java class, which allows for low-level memory manipulation. They said it was “less safe than C” (or C++?) if you can believe that. 😛

---

<div class="post-metadata">

### Author: ![MIha\_Markic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/miha_markic/32/11397_2.png) [@MIha\_Markic](https://discuss.kotlinlang.org/u/MIha_Markic)
#### Post date: [July 19, 2026, 11:50am UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/5 "2026-07-19T11:50:00Z")

</div>

> AFAIK Kotlin itself doesn’t have any “unsafe” operations, but the JVM does. There was another thread here where somebody mentioned the `Unsafe` Java class, which allows for low-level memory manipulation.

Yep, that’s it, I suppose.

> They said it was “less safe than C” (or C++?) if you can believe that. 😛

I’ll use protection gloves and goggles when accessing that class. 🙃

---

<div class="post-metadata">

### Author: ![frozenice](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/frozenice/32/7436_2.png) [@frozenice](https://discuss.kotlinlang.org/u/frozenice)
#### Post date: [July 20, 2026, 10:48am UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/6 "2026-07-20T10:48:55Z")

</div>

Heads up: `Unsafe` will be going away (mostly), there are better alternatives:

- VarHandles for field access / CAS / etc.
- FFM API for native memory

Only `allocateInstance` will remain.

See e.g. [this article](https://foojay.io/today/unsafe-is-finally-going-away-embracing-safer-memory-access-with-jep-471/) for an overview and [JEP 471](https://openjdk.org/jeps/471) etc. for details.

---

<div class="post-metadata">

### Author: ![davidecannizzo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/davidecannizzo/32/6549_2.png) [@davidecannizzo](https://discuss.kotlinlang.org/u/davidecannizzo)
#### Post date: [July 22, 2026, 11:37am UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/7 "2026-07-22T11:37:16Z")

</div>

Hahaha, that was me! I still stand by that notion. Who would imagine people would remember that statement?

Yes, C is weakly typed, while the JVM `Unsafe` class isn’t typed at all, and almost any possible usage of it relies on behavior not defined in the JVM specs (because those are only concerned with Java’s own safe memory model), basically private details of the specific JVM implementation itself that might change over time, such as class layout.

---

<div class="post-metadata">

### Author: ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)
#### Post date: [July 22, 2026, 12:10pm UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/8 "2026-07-22T12:10:30Z")

</div>

It stuck in my memory for two reasons; firstly, because I didn’t even know Java had `Unsafe` memory stuff, and secondly, because the concept of Java doing _anything_ worse than C (except for performance) was just a totally wild concept to me.

---

<div class="post-metadata">

### Author: ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)
#### Post date: [July 30, 2026, 12:25pm UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/9 "2026-07-30T12:25:16Z")

</div>

The fully-qualified name of the `Unsafe` class is `sun.misc.Unsafe`; AIUI, this means it’s specific to Sun/Oracle-based JVMs, and probably isn’t available on other JVM implementations.

So non-Sun/Oracle code shouldn’t be using it in the first place, should it?

---

<div class="post-metadata">

### Author: ![davidecannizzo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/davidecannizzo/32/6549_2.png) [@davidecannizzo](https://discuss.kotlinlang.org/u/davidecannizzo)
#### Post date: [July 30, 2026, 12:53pm UTC](https://discuss.kotlinlang.org/t/is-there-c-unsafe-code-like-in-kotlin/31138/10 "2026-07-30T12:53:49Z")

</div>

Yeah, `sun.misc.Unsafe` has never truly been standard, although it’s been very commonly present and a ton of popular libraries have built around it.

From JDK 22 onward, the `java.lang.foreign` API should provide a better replacement where explicit control over memory access is needed.
