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? 4 8452
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?
"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
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.c om/info/cpyright.htm"
"Pieter Philippaerts" <Pi****@nospam. mentalis.org> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l... "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
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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 following code is executed?
object test = new object();
int temp = Marshal.SizeOf(test);
|
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. unmanaged
Boolean
what is correct syntax?
|
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?
thanks
Claire
|
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 types (like int32, etc).
Thanks
| |
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 in advance,
chris
|
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 namespace, but it fails when
passed a managed type. I poked around the Reflection namespace, but found
nothing that fits the bill. Is there a runtime equivalent to sizeof in
..NET?
|
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 work on all
implementation.
size_t size_obj = (char*)(&obj + 1) - (char*)(&obj);
I wanted to find out on which implementation this would fail. One of
|
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 sizeof or Marshal.SizeOf(), since apparently these
functions are for unmanaged code.
With references and managed languages, what do you use to figure this
out?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |