The System.Char dimension

  • Thread starter Thread starter RicercatoreSbadato
  • Start date Start date
R

RicercatoreSbadato

I've read that the size of char or of the System.Char under .NET is 2
bytes.

Then when I do:

int char_len = Marshal.SizeOf(Type.GetType("System.Char"));

char_len would be 2!

Instead the value is 1!

Where is the error?
 
Read the documentation for Marshal.SizeOf... it's explained there.
 
ok, from MSDN: "The size returned is the actually the size of the
unmanaged type. The unmanaged and managed sizes of an object can
differ."

But how can I take the real size of a TYPE ?
 
RicercatoreSbadato said:
ok, from MSDN: "The size returned is the actually the size of the
unmanaged type. The unmanaged and managed sizes of an object can
differ."

But how can I take the real size of a TYPE ?

Why would you want to know?

You can never access the managed type as a piece of memory so it is never
useful to know its size.

The only half-plausible reason is that you want to assess the actual memory
requirements of your class but even then this is impossible for any
reasonably complicated class because of stuff like overheads in library
hashtables which might vary with the data.
 
Marshal.SizeOf returns the size of the marshaled version of the type.

What you need is this:
sizeof(char)

note however that this is only valid for value types, there is no reliable
way to obtain the size of reference types.

Willy.

| I've read that the size of char or of the System.Char under .NET is 2
| bytes.
|
| Then when I do:
|
| int char_len = Marshal.SizeOf(Type.GetType("System.Char"));
|
| char_len would be 2!
|
| Instead the value is 1!
|
| Where is the error?
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top