sizeof

  • Thread starter Thread starter Brad Williams
  • Start date Start date
B

Brad Williams

Why can't you take the sizeof a class? Would there be some implementation
problem for the compiler writers, or is it to stress that there is just no
point in knowing the size (since you obviously could never do pointer
arithmetic with reference-type objects)? I don't see how there would be an
implementation problem.
 
Hi Brad!

it's reserved for unmanaged code. use
System.Runtime.InteropServices.Marshal.SizeOf() instead.
 
Thanks.

Why is the C# language designed such that sizeof can't work with ref types?
 
Actually C# has a sizeof but it only applies to value types. The
System.Runtime.InteropServices.Marshal.SizeOf() refers to the unmanaged size
of something. The reason the sizeof operator applies only to value types I
believe is that value types are allocated on the stack while classes are
allocated on the GC heap and thus come under the dynamic class loader of
Fusion which can change the memory layout as it sees fit.

--
--------------------------------------------------------------
Sam Gentile [C#/.NET MVP]
..NET Blog http://samgentile.com/blog/
MSDN Column:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/bridge.asp
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top