Questions about Structures

MSDN says that a class that would be 16 bytes or less is best treated as a struct [citation] .
Why is this? Does this mean that if the structure is more than 16 bytes, is it less efficient than the class, or is it the same?
How do you determine if your class has less than 16 bytes?
What limits the structure from acting as a class? (also to forbid constructors without parameters)

+7
c # struct class
source share
7 answers

There are several different answers to this question, and this is a bit subjective, but some of the reasons I can think of are as follows:

  • Structures are a type of values, classes are referential. If you use 16 bytes for shared storage, you probably should not create memory records (4 to 8 bytes) for each of them.
  • When you have really small objects, you can often push them onto the IL stack rather than referring to objects. This can really speed up some code as you eliminate the memory difference on the called party.
  • There is a little extra fluff in IL related to classes, and if your data structure is very small, none of these puffs will be used anyway, so this is just extra garbage that you do not need.

The most important difference between a structure and a class is that structures are a value type, and classes are a reference type.

+6
source share

Under “effective,” they probably talk about the amount of memory that is required to represent a class or structure.

On a 32-bit platform, a minimum of 16 bytes is required to allocate an object. On a 64-bit platform, the minimum size of an object is 24 bytes. So, if you look at it exclusively from the amount of memory used, a structure containing less than 16 bytes of data will be "better" than the corresponding class.

But the amount of memory used is not the whole story. Types of values ​​(structure) are fundamentally different from reference types (classes). Structures can be inconvenient to work with and can actually cause performance problems if you are not careful.

The real answer, of course, is to use what works best in your situation. In most cases, you will be much better off using classes.

+3
source share

Check out this link, I found it on one of the answers in SO today:. Internal types NET . You can also try searching for SO and Googling for “reference types and value types” for the differences between structures and classes.

What limits the structure from acting as a class?

There are many differences. You cannot inherit from a structure, for example.

You do not have virtual methods, so you cannot use struct to implement an interface. Instance methods in structures can access private struct fields, but in addition they behave as auxiliary "auxiliary" functions (for immutable structures, they sometimes do not even need access to personal data). Therefore, I believe that they are not as close as “valuable” as class methods.

+2
source share

In memory, the structure will store data directly, while the class will behave more like a pointer. This in itself is important because passing the structure as a parameter to the method will pass its values ​​(copy them onto the stack), and the class will pass a reference to the values. If the structure is large, you will copy many values ​​for each method call. When it is really a small copy of values, and using them directly will probably be faster than copying the pointer and grabbing it from another place.

About restrictions: you cannot assign it to zero (although you can use Nullable <>), and you must initialize it immediately.

0
source share

This is because the CLR processes structures and classes. Structures are value types that mean they live on the stack, not on a managed heap. This is a good rule to keep structures small, because as soon as you start passing them as arguments to the method, you will incur overhead when the structures are copied as a whole when passed to the method.

Because classes pass a copy of their method reference, they carry much less overhead when used as arguments to a method.

The best way to determine the size of your class is the total number of bytes needed by all members of your class, plus an additional 8 bytes for CLR overhead (synchronization block index and object type reference).

0
source share

Structures differ from classes because they are stored on the stack, not on the heap. This means that every time you call a method with the struct parameter as a parameter, a copy is created and passed to the method. That is why large structures are extremely inefficient.

I would actively discourage the use of structures, though, because it could cause some subtle errors: for example. when you change the structure field, it will not be displayed to the caller (because you just changed the copy) - this is a completely different behavior for classes.

So, 16 bytes, I think, is a reasonable maximum size of the structure, but in most cases it is better to have a class. If you still want to create a structure, try to make it unchanged at least.

0
source share

Copying an instance of the structure takes less time than creating a new instance of the class and copying data from the old one, but class instances can be shared, and struct instances cannot. Thus, “structvar1 = structvar2” requires copying a new instance of the structure, while “classvar1 = classvar2” allows classvar1 and classvar2 to refer to the same instance of the structure (without the need to create a new one).

The code for processing new instances of the structure is optimized for sizes up to 16 bytes. Larger structures are processed less efficiently. Structures - this is a gain in cases where each variable containing the structure will contain an independent instance (i.e. there is no reason to expect that any two separate variables will contain the same instance); they don’t really win (if they win at all) in cases where many variables can contain the same instance.

0
source share

All Articles