Charsets and marshaling

The way char values, string objects, and System.Text.StringBuilder objects are marshaled depends on the value of the CharSet field on either the P/Invoke or structure. You can set the CharSet of a P/Invoke by setting the DllImportAttribute.CharSet field when declaring your P/Invoke. To set the CharSet for a structure, set the StructLayoutAttribute.CharSet field on your struct declaration. When these attribute fields are not set, it is up to the language compiler to determine which CharSet to use. C# and VB use the Ansi charset by default.

The following table shows a mapping between each charset and how a character or string is represented when marshaled with that charset:

CharSet value Windows .NET Core 2.2 and earlier on Unix .NET Core 3.0 and later and Mono on Unix
Ansi char (ANSI) char (UTF-8) char (UTF-8)
Unicode wchar_t (UTF-16) char16_t (UTF-16) char16_t (UTF-16)
Auto wchar_t (UTF-16) char16_t (UTF-16) char (UTF-8)

Make sure you know what representation your native representation expects when picking your charset.