The purpose of this article is to understand different behaviours in Marshal.SizeOf and sizeof operator for boolean and char data types in C#
sizeof operator
The sizeof operator takes a type name and tells you how many bytes of managed memory need to be allocated for instance of struct. The sizeof operator in C# works only on compile-time known types, not on variables (instances)
Marshal.SizeOf operator
Marshal.SizeOf takes either a type object or an instance of the type, and tells you how many bytes of unmanaged memory need to be allocated. Marshal.SizeOf can be used on any object instances or runtime types.
Code
The inline code below explains the difference in the bytes for int and char types based on the understanding in the introduction section.
You've successfully subscribed to Coding Today
Great! Next, complete checkout for full access to Coding Today
Welcome back! You've successfully signed in.
Unable to sign you in. Please try again.
Success! Your account is fully activated, you now have access to all content.