471,321 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,321 software developers and data experts.

The System.Char dimension

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?

Feb 1 '06 #1
5 1664
Read the documentation for Marshal.SizeOf... it's explained there.
--
http://www.kynosarges.de
Feb 1 '06 #2
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 ?

Feb 1 '06 #3

"RicercatoreSbadato" <em***********@yahoo.it> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.
Feb 1 '06 #4
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.

"RicercatoreSbadato" <em***********@yahoo.it> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
| 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?
|
Feb 1 '06 #5
tnx ;)

Feb 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Rafal Dabrowa | last post: by
11 posts views Thread by miketigerwoods | last post: by
2 posts views Thread by Mirandole | last post: by
5 posts views Thread by Jackson | last post: by
1 post views Thread by newstips6706 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.