tag:blogger.com,1999:blog-6698224.post-1080671367033321052004-03-30T12:19:00.001-06:002004-08-11T10:30:11.570-05:00Marshal.SizeOf returns 1 for System.Char<span style="font-size:85%;"><span style="font-family:trebuchet ms;">As you might know, in .NET all strings are Unicode, because of that, the size of a System.Char type must be 2 bytes. When you try to get the size in bytes of a System.Char using the Marshal.SizeOf method, you will find that it return 1 instead of 2. <br />This is not a bug, Marshal.SizeOf was made for unmanaged types, not for managed types as System.Char. Marshal.SizeOf gets the size of the System.Char type declared in the mscorlib assembly, if you check its declaration using the "Lutz Roeder's .NET Reflector" you will notice that the System.Char type has the following modifiers: <br /> <br /><em>Modifiers: Serializable BeforeFieldInit </em><strong>Ansi</strong><em> Auto </em> <br /> <br />By this reason, the size of any Ansi character <strong>is 1</strong>. In order to get size of a character, you should use the value of <strong>Marshal.SystemDefaultCharSize</strong>.</spam></span></span> <br />Danielhttp://www.blogger.com/profile/17636031092965440114noreply@blogger.com