Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

!sizeof

Back to Forum: Tech Off
  • User profile image
    thumbtacks2

    In C#, it's my understanding that variables are created as objects. Does anybody know how large those objects are? In C, you have the sizeof operator, and it returns the size of an object in bytes. But in C#, from what I read here, it's sizeof operator "can only be used in the unsafe mode".  Hmmm. Is there an alternate keyword that serves the same purpose?

    Also, how does C# perform when you are manipulating large sets of data? Or is that not recommended. Just an academic interest on my end...nothing serious planned.

  • User profile image
    W3bbo

    Getting the runtime size of a Managed object depends on a lot of factors, but there are some classes in System.Interop, primarily for marshalling between Managed and Non-Managed code. Generally speaking, you'll never need to touch it.

    What do you need the size of an object for? With the exception of structs like (U)Int* everything is on the heap and stored as references. So just multiply the class's fieldcount by the size of IntPtr... pointless (and totally inaccurate) measurement IMO.

  • User profile image
    Yggdrasil

    thumbtacks2 wrote:
    In C#, it's my understanding that variables are created as objects.


    That depends. Primitive types are primitive and have a fixed size - ints are 32bit, longs are 64bit and so forth.  Here's a reference of the C# primitive types and their sizes.

    It might also be a good idea to read up on boxing and unboxing to understand the meaning of an integer being both a primitive value type and also being an object inheriting from System.Object. Here's the MSDN lowdown. In a nutshell, an int will be a stack-allocated value as long as you treat it as such. Once you decide to treat it as an object (casting, polymorphism and the like) it will automatically be wrapped in a heap-allocated object.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.