Skip to content

Going Unmanaged

A Hands-On C++ Handbook for C# Developers

You have spent years in managed code — the runtime tracked your objects, the GC cleaned up after you, and "unmanaged" was the scary word in the P/Invoke docs. This handbook is the other side: the concepts, the labs you do cold, and the reference you keep open once you are there.

Where to read it: mkhomutov.github.io/going-unmanaged — the site is built from these files, and it is where the code listings render; on GitHub they show as the include directives that pull them from the tested sources.

Who this is for: developers with solid C# (or Java) experience who once knew C++ or are learning it now, and need to become productive in a real C++ codebase — typically one built around a vendor SDK: a plug-in API for a desktop application, a peripheral-device SDK, a game or media engine, an embedded HAL. Each chapter therefore ends with an "In the wild" section connecting the concept to the C-flavored APIs you will actually meet, and Part V trains on two miniature SDKs written in those idioms.

How to use it: by the question in front of you, through the Reference list below — the symptom index, the cookbook, the choosing procedures and the catalogues. By topic, through the Concepts, when one surfaces at work. By doing, through the Labs, cold. Read in order, the Parts are the syllabus: I–IV the language and the toolchain, V the exercises and the mistakes they produced, VI the real codebase — what a project has that an exercise does not. The appendices are the survival kit: the fundamentals refresher, the one-page cheat sheet for any morning, the curated resources, the glossary, the cookbook indexed by the C# API you are reaching for, the bridge catalogue for the meeting where the plug-in must speak to everything else, the choosing procedures for the four decisions every signature makes, const-correctness as one subject rather than five fragments, the CMake catalogue for the verb already in your head, and the standards catalogue for the spelling a newer codebase uses and the way to ask a toolchain which standard it speaks.

For the Java reader: the comparisons are written in C#, but most of them rest on facts your runtime shares — a GC, objects behind references, single inheritance, an Object root, immutable strings, exceptions as the culture — so they translate on sight, and the code always carries the lesson on its own. Read with this pocket dictionary once and you will rarely need it again: using/IDisposable → try-with-resources/AutoCloseable; basesuper; sealedfinal; internal → package-private; delegates and event → functional interfaces and listener registration; LINQ → Streams; Task.Run + awaitExecutorService + Future.get; NuGet → Maven/Gradle; ArgumentNullException/InvalidOperationExceptionNullPointerException/IllegalStateException. Three places the languages genuinely diverge, flagged in place when you get there: C# has value types — when Chapter 2 says everything assigns like a C# struct, read like a Java primitive: the whole object copied on assignment, fields and methods included; Java has no such type, and this one anchor you must build rather than borrow. C# generics are reified, so Chapter 7's baseline is the opposite of your erasure instinct — though C++ templates' T-is-gone-at-runtime half will feel like home. C# methods are non-virtual by default, like C++ — so in Chapter 5 it is your everything-is-virtual reflex, not the C# reader's, that is the dangerous one.

Contents

Three ways in. Reference is for the question in front of you; Concepts for the topic that has surfaced; Labs for the work you do cold. The reading order below them is the sequence the chapter numbers follow.

Reference

Concepts

Labs

Each has a task card under exercises/ so it can be attempted without the solution on the next screen; the six from Chapter 32 on are tickets — a symptom and the code it happened to, the diagnosis behind a fold — and so are Chapters 44 and 45, the last of which is the one lab that starts from code that works.

Reading order

Part I — The Mental Shift

  1. Ownership and RAII — who frees this, and the destructor that guarantees it
  2. Value Semantics — assignment copies, and what that costs you
  3. Stack, Heap, and Undefined Behavior — where a variable lives, and the bugs that don't announce themselves

Part II — The Language, Side by Side

  1. Classes, Inheritance, Interfaces — the same words as C#, under different rules
  2. Virtual Dispatch and the Virtual Destructor — one missing keyword, and every derived destructor skipped
  3. The Rule of Five and Move Semantics — owning a raw resource without double-freeing it
  4. Templates vs C# Generics — compile-time code generation, not one runtime type
  5. Error Handling: Exceptions and Error Codes — two dialects of one language, and how to choose
  6. Casts, Conversions, and Strings — four casts by name, and a string that knows no encoding

Part III — The Standard Library

  1. Modern C++ Fluency — auto, lambdas, optional and variant: the C++ that reads like C#
  2. STL Containers, Algorithms, and Iterator Invalidation — the Dictionary equivalents, and the loop that eats itself

Part IV — The Build and the Toolchain

  1. The Compilation Model — why the error came from the linker
  2. Toolchain Quick Reference — the flags, and what MSVC calls the thing you know

Part V — Learning by Doing

  1. Exercise: The Lifetime Tracer — seeing every copy, move, and death
  2. Exercise: The Buffer — the Rule of Five, for real
  3. The SDK Bestiary — the shapes vendor APIs take in the wild
  4. Exercise: The FakeSDK — error codes and owned payloads (desktop-app style)
  5. Exercise: The Device SDK — opaque handles and C callbacks (peripheral style)
  6. Exercise: The Word Counter — STL fluency end to end
  7. Exercise: Slicing and Polymorphism — the container that loses your data
  8. Exercise: Iterator Invalidation — mutating while iterating, safely
  9. Exercise: Lambda Lifetimes — captures that outlive their scope
  10. Exercise: The Build-Model Lab — provoking and reading every error stage
  11. Retired. The number is not reused.
  12. Gotchas: the Findings Log — the mistakes practice produced, numbered and append-only: symptom, theory, broken and fixed code, habit

Part VI — The Real Codebase

  1. Build Systems and CMake — what builds your code when one command no longer will
  2. Dependency Management — there is no NuGet, and why that follows from the ABI
  3. Testing — build a framework in forty lines, then learn why assertions aren't enough
  4. Concurrency — no runtime, no await, and the thread that calls you back
  5. Authoring an ABI Boundary — the other side of the Bestiary: shipping the thing someone else loads
  6. Reading What the Tools Tell You — sanitizer reports line by line, and the debugger and profiler skills that differ
  7. Crash on Exit — a crash after main returns that support cannot reproduce — work it cold
  8. A Value Reads Zero After Hot-Plug — a sanitizer report arrives attached, and the guilty line is in none of its stacks
  9. Every Capture Rejected as Malformed — an attached hex dump the vendor's viewer decodes fine and your parser doesn't — while every sanitizer stays green
  10. Objects Still Live at Unload — the vendor's 2.0 is refcounted, and the host says objects are still live at unload
  11. Dropouts With the Plug-in Loaded — a click every few minutes with your plug-in loaded, and an attached profile that says you are innocent
  12. Crash at Session Close, Field Units Only — a crash report from a machine you will never see, and every bench rig is green
  13. The Bridge Out — serving C#, Python, and the rest from inside the host: one queue, one seam, and a deadline on every wait
  14. The Round Trip Home — P/Invoke from the native side: a signature written twice in two languages, and the four things nothing checks
  15. CMake for the Plug-in — a MODULE with one exported symbol, an SDK that ships no config package, and the build decisions Chapter 26 never had to make
  16. Templates You Will Write — the working subset: the seam as a template parameter, static_assert as the judge, the three utilities, and how to read the error
  17. The Formula Field — user-typed text evaluated against the host's objects: tokens as a closed set, a tree behind unique_ptr, recursive descent with a value at every level, the provider seam, and a judge whose oracle is a table worked out by hand
  18. Below the Mutex — the hand-off to a deadline thread once a lock is forbidden: a bounded ring with one producer and one consumer, the two memory orders that make it correct and what they cost per instruction set, and the interrupt handler as the same rule with waiting removed
  19. Crash at Document Close — a C++-native framework with an object model of its own, and the ownership clean-up that put a second owner on every node: the ticket the Bestiary's fifth shape had never been worked as
  20. The Callers Must Not Notice — the retrofit: a working 2009 class modernised one seam at a time, with the callers' files untouched and their output byte-identical, and the promise nobody wrote down that the obvious seam would have broken

Appendices

  • A. Fundamentals Refresher: pointers, references, explicit, = delete, const, .lib files, signed vs unsigned and size_t, and naming
  • B. Core Principles — the one-page cheat sheet
  • C. Retired. The letter is not reused.
  • D. Resources and Further Reading
  • E. Glossary — the terms of art on one page, each pointing at its owning chapter, plus the ones colleagues use without introduction
  • F. The Rosetta Cookbook — everyday tasks, indexed by the C# reflex and its Java neighbour
  • G. The Bridge Catalogue — Chapter 38's lookup half: every way to connect a foreign client to a native host, priced, with the decision table
  • H. Choosing: Signatures, Containers, and Storage — the four decisions in every signature: which container, how to take a parameter, what to return, and what goes inside the collection
  • I. Const-Correctness — const as one subject: a path rather than an object, the interface it splits in two, and what retrofitting it costs
  • J. The CMake Catalogue — Chapters 26, 27 and 40's lookup half: every verb they teach and the page that owns it, then the handful none of them needed, priced, with the decision table
  • K. The Standards Catalogue — which standard a toolchain is speaking and how to ask it, every feature the book names by the standard it arrived in with the newer spelling beside the one taught, and the standards in one sitting
  • L. What Things Cost — Chapter 36's lookup half: what a copy, a throw, a shared_ptr and a heap allocation cost, which instrument answers which complaint, and the two rows nobody had written down — an arena, and the cache line two counters share
  • M. Field Questions — the index of the questions asked mid-task with no one to ask, each restated as a reader would search for it and pointing at the chapter section, recipe or finding that answers it

License: this text is © 2026 Maksim Khomutov and licensed under CC-BY 4.0 — share and adapt it with attribution to "Going Unmanaged — A Hands-On C++ Handbook for C# Developers" and a link to the repository. The code is MIT, including every code sample below: paste it into your own work freely.