On Wed, Apr 16, 2008 at 10:10 PM, Sébastien Lorion <[EMAIL PROTECTED]> wrote: > On 4/17/08, Curt Hagenlocher <[EMAIL PROTECTED]> wrote: > > On Wed, Apr 16, 2008 at 8:31 PM, Sébastien Lorion > > > > <[EMAIL PROTECTED]> wrote: > > > > > I want the managed size, so Marshal.SizeOf does not return the correct > > > value. > > > For example, sizeof(char) returns 2 and Marshal.SizeOf(typeof(char)) > > returns 1. > > > > Huh. So it does. Are there any other types for which this is true? > > Well, basically every types ... Marshal.SizeOf returns the size of the > equivalent unmanaged type. Another example would be boolean: Marshal > returns 4 instead of 1.
As per the table below, "every type" may be a bit of an exaggeration :). Of course, even one exception makes the technique useless. If you're calling it often enough so that there really is a significant perf gain to be had by using Buffer.BlockCopy over Array.Copy, why not create a Dictionary<Type, int> and populate it with all the values you expect to encounter? type marshal sizeof char 1 2 bool 4 1 byte 1 1 sbyte 1 1 int16 2 2 uint16 2 2 int32 4 4 uint32 4 4 int64 8 8 uint64 8 8 float 4 4 double 8 8 decimal 16 16 IntPtr 4 4 (on a 32-bit OS, uses IntPtr.Size instead of sizeof) IntPtr 8 8 (on a 64-bit OS, uses IntPtr.Size instead of sizeof) > I wanted to use Buffer.BlockCopy in a class with generic parameters > and I could not use sizeof(T). Which makes sense, because "sizeof" is a compile-time operator while template instantiation happens at run time. -- Curt Hagenlocher [EMAIL PROTECTED] =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com