How to calculate how large a structure or class is?

I was told that I should only use structures when they are less than 16 bytes. If more, using the class would be more optimal.

I was wondering how can I solve this?

Am I just adding all the fields that are in the structure?

For example, if this struct

public struct Struct1
{
    int int1;
}

Then, if it has one integer and one int is 32 bits, then is it four bytes?

How about if I have many methods in this structure? Will the method add to the size of the structure?

+3
source share
5 answers

Only non-static variables use space, but methods do not. For example, your structure that you created there has four large bytes because it is an int size.

, Marshal.SizeOf(GetType ((Struct1)).

, , .

+3

sizeof.

EDIT: Nevermind, . (, )

+2

16bytes , . . , . , .

0

: ?

MSDN : . , :

    * It logically represents a single value, similar to primitive types (integer, double, and so on).
    * It has an instance size smaller than 16 bytes.
    * It is immutable.
    * It will not have to be boxed frequently.
0

, , , - , .

, , ( MSDN 2005 , - ), CLR , ; , , , , :

http://msdn.microsoft.com/en-us/magazine/cc163791.aspx

0

All Articles