473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

sizeof(), C# and managed code

I'm porting a C++ function which reads a file in binary mode, sometimes
reading 2 bytes into an unsigned short, 4 bytes into a long, etc into a C#
implementation. When using the FileStream.Read function, I'd like to code
it to read the size of a C# ushort, for example. The documentation says the
sizeof() is only supported in unsafe mode. Is there any equivalent to
sizeof() that I can use in managed code?
Nov 16 '05 #1
4 8431
Hi Paul,

If you use BinaryReader, you have several methods to read in various
size value types, such as ReadInt16, ReadInt32, ReadDouble, etc.

Thobias Jones
Paul wrote:
I'm porting a C++ function which reads a file in binary mode, sometimes
reading 2 bytes into an unsigned short, 4 bytes into a long, etc into a C#
implementation. When using the FileStream.Read function, I'd like to code
it to read the size of a C# ushort, for example. The documentation says the
sizeof() is only supported in unsafe mode. Is there any equivalent to
sizeof() that I can use in managed code?

Nov 16 '05 #2
"Paul" <no*****************@i-stat.com> wrote
The documentation says the
sizeof() is only supported in unsafe mode. Is there any equivalent to
sizeof() that I can use in managed code?


Try the Marshal.SizeOf() method (from the System.Runtime.InteropServices
namespace).

Regards,
Pieter Philippaerts
Nov 16 '05 #3
You shouldn't use Marshal.SizeOf() for this operation. I assume that Paul
is trying to read managed types from the file. Marshal.SizeOf() will give
you the unmanaged size which may not be equal to
managed size.

--
Jared Parsons [MSFT]
ja******@online.microsoft.com
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
"Pieter Philippaerts" <Pi****@nospam.mentalis.org> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
"Paul" <no*****************@i-stat.com> wrote
The documentation says the
sizeof() is only supported in unsafe mode. Is there any equivalent to
sizeof() that I can use in managed code?


Try the Marshal.SizeOf() method (from the System.Runtime.InteropServices
namespace).

Regards,
Pieter Philippaerts

Nov 16 '05 #4
Paul <no*****************@i-stat.com> wrote:
I'm porting a C++ function which reads a file in binary mode, sometimes
reading 2 bytes into an unsigned short, 4 bytes into a long, etc into a C#
implementation. When using the FileStream.Read function, I'd like to code
it to read the size of a C# ushort, for example. The documentation says the
sizeof() is only supported in unsafe mode. Is there any equivalent to
sizeof() that I can use in managed code?


Aside from the other answers, you don't need to use sizeof to find the
size of a ushort - it's *always* 16 bits, just like an int is *always*
32 bits. The C# designers learned from the C experience that leaving
the sizes of primitive data types somewhat ambiguous is a bad idea :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alison | last post by:
Hi, I am new to C#. Just wondering how to measure the size of struct I defined in managed code(in bytes). How can I use sizeof to achieve that ? Thanks
2
by: Beringer | last post by:
Why do I get the following run time error: Additional information: Type System.Object can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed. When the...
6
by: Brian Keating | last post by:
Hi there, i want to know size of managed bool (know it is 1byte according to language spec) but how do i do it in code? Marshal.Sizeof... isn't an option cause it returns 4 bytes, i.e....
1
by: Claire | last post by:
char a = 'p'; if (System.Runtime.InteropServices.Marshal.SizeOf(a) == 1) dothis(); else dothat(); SizeOf(a) is returned as 1. I thought chars were 16 bits in size. Why is it returning 1 to me?...
9
by: Salvador | last post by:
Hi, Does anybody know how to get the size in bytes of a managed type? I have a hashtable and I want to check the size in bytes. I can get the size using sizeof for value types with prmitives...
12
by: Christopher Pragash | last post by:
Hello All, Is there an equivalent of SizeOf operator in VB.NET? What I'm trying to achieve is to determine at runtime what the size of hashtable is in terms of the memory it occupies. Thanks...
15
by: stand__sure | last post by:
Having recently had a need to use ZeroMemory from the kernel32.dll, I "discovered" that there is apparently no .NET equivalent to the c/c++ sizeof operator -- one does exist in the Marshall...
20
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to...
4
by: raylopez99 | last post by:
Marshal..::.SizeOf Method (Object) and sizeof() do not exist for objects in a managed language, right? I was trying to find how much memory is taken up by an array of ints, and I could not use...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.