Sign in to follow this  
qux

[.net] C# char size

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this