Porting C# Patterns

Quick mapping from C# managed concepts to deliberate native C++ Sharp Runtime patterns.

GuidesSource 6d429559

Common value mappings

C#Sharp Runtime / C++ directionImportant difference
stringSystem::String helpers over std::stringUTF-8 bytes, not a managed UTF-16 object
bool, integer aliasesboolcs, intcs, longcs and related aliasesC++ overload and promotion rules remain visible
charcharcs / char16_tDo not confuse a UTF-16 code unit with a UTF-8 byte
T?Nullable<T> / optional-value patternContained values use native construction and copying
reference typeValue, unique_ptr, shared_ptr, weak_ptr, reference or pointerThe port must choose ownership explicitly

Pattern map

C# conceptNative porting directionCompatibility boundary
Managed class referenceExplicit owner: value, unique/shared pointer, or borrowed referenceNo GC or universal object identity
PropertyNamed getter/setter or the project’s property helper where establishedA helper is compile-time C++; it creates no runtime metadata
Action/Func/PredicateTyped callable, principally std::function-shapedCaptures have ordinary C++ lifetime
EventTyped callback collection plus explicit subscription protocolPublisher/subscriber lifetime is not managed
ExceptionSpecific Sharp Runtime exception when the contract existsC++ RAII runs during unwinding; no CLR stack services
List/DictionarySelect Collections.Core and decide value/identity ownershipMutation, iterator and comparer rules must be verified
async/awaitExplicit Task/continuation/cancellation flow—or idiomatic native concurrencyNo C# language syntax or ambient synchronization context
ReflectionExplicit registry, templates, variants or typed factoriesType is basic RTTI, not a metadata universe
Activator.CreateInstance<T>Compile-time templated activation where supportedNo general Type-driven construction
IDisposableRAII plus compatibility-shaped Dispose where neededDestructors remain the safety net
P/Invoke / COMWrite an explicit native boundary outside the compatibility facadeNo CLR marshaller or DLL-binding service

Workflow

  • Inventory observable behavior and managed-only dependencies.
  • Design ownership and concurrency before translating syntax.
  • Select the narrow Sharp Runtime components.
  • Port one behavior slice with tests.
  • Record reductions explicitly where exact parity is impossible or intentionally omitted.