Porting C# to Native C++
A systematic workflow for porting C# code with Sharp Runtime while respecting native ownership and compatibility boundaries.
1. Inventory managed assumptions
- Reference identity and object graph ownership
- GC-dependent lifetime or finalization
- Reflection, serialization, DynamicInvoke or AppDomain use
- async/await, synchronization context and cancellation
- UTF-16 indexing and culture-sensitive text
- TLS, certificates, P/Invoke or platform services
2. Select compatibility surfaces
Map the required APIs to physical components. Keep deliberate rewrites—such as reflection registries or secure transport—outside a false compatibility layer.
3. Design native ownership
Choose values, unique owners, shared owners and borrows. Sketch callback and async lifetime before implementation.
4. Translate behavior slices
Port one cohesive class or subsystem with its boundary tests. Preserve observable validation and exception behavior only where the implemented Sharp Runtime contract supports it.
5. Verify reductions
| Risk | Verification |
|---|---|
| String indices | Non-ASCII and malformed UTF-8 cases |
| Numeric behavior | Overflow, NaN, infinity, signed zero and compiler capabilities |
| Collections | Mutation/enumerator invalidation and comparer policy |
| I/O | Short operations, lifecycle, permissions and platform paths |
| Concurrency | Cancellation, teardown, exception observation and races |
| Networking | DNS/connect/protocol distinctions and TLS boundary |
6. Prefer idiomatic C++ when compatibility adds no value
Sharp Runtime is a tool for practical ports, not a requirement to preserve every managed design. A native value, standard range or explicit factory is often clearer than emulating a managed-only mechanism.