[.net] C# char size

Started by
5 comments, last by VizOne 15 years, 11 months ago
So im curious, why does sizeof report chars as being 2 bytes, but when i write to a binary file they only take up one byte?
Advertisement
Because they're set to marshal as Ansi (single byte) for some insane reason.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Or the encoding is set to UTF-8, which causes characters in the ASCII range to only occupy one byte.

For example, the default setting of BinaryWriter is to use UTF-8. This is a reasonable choice as it will reduce the file size (for texts that use mostly ASCII characters at least). You can make BinaryWriter use a different encoding (e.g. Unicode) by using the appropriate constructor overload.

Regards,
Andre
Andre Loker | Personal blog on .NET
It could be worse, BinaryWriter could default to use Encoding.Default ;)
Also isn't UTF-8 more endian friendly ? I know UTF-8 is my prefered choice of file storage.
What's really crazy is when you are writing applications in C++/CLI which target the .NET framework, and the sizeof() operator returns a different size for char than Marshal.SizeOf().
Mike Popoloski | Journal | SlimDX
interesting, thanks :)
Quote:Original post by Mike.Popoloski
What's really crazy is when you are writing applications in C++/CLI which target the .NET framework, and the sizeof() operator returns a different size for char than Marshal.SizeOf().


The same happens in C# (Marshal.SizeOf(typeof(char)) vs sizeof(char)). It's caused by the fact that the CLR assumes CharSet.Ansi if the given type has no CharSet in the StructLayout attribute. System.Char has no StructLayoutAttribute.

Regards,
Andre
Andre Loker | Personal blog on .NET

This topic is closed to new replies.

Advertisement