Primitive types
Read time 1 minuteLast updated 11 days ago
Primitive types are small value types that are copied to the stack (or a CPU register) when you use them as function parameters or return values. You don't need to pin or otherwise manage the lifespan of primitive types when calling unmanaged functions.
C# has different names for many primitive types compared to C and other languages. Refer to Platform invoke data types for a list of the unmanaged C language types that correspond to the managed C# types.
The following table shows the C type that each C# type marshals to by default. Default marshalling is a starting point, not a guarantee: it can vary by platform and by context (parameter, return value, or struct field). For example, marshals to a 4-byte value by default, and marshals according to the of the call. Use MarshalAsAttribute to control the exact representation when the default isn't what you need.
System.BooleanSystem.CharCharSetSome examples include:
C# base type | C# alias name | Unmanaged C type |
|---|---|---|
| System.Int32 | int | int32_t |
| System.Single | float | float |
| System.Char | char | char |
| System.Boolean | bool | int32_t |
| System.Byte | byte | unsigned char |