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.
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.
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.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.