Home c# Structures in C # and its advantages

Structures in C # and its advantages




Where can I use structures? And in what situations it is better to resort to structures than to classes?

Answer 1, Authority 100%

free translation piece hence

Struct Is a Value Type, SO IT IS Faster Than A Class Object. Use
Struct Whenever You Want to Just Store The Data. Generally, structs
Are Good for Game Programming. HOWEVER, IT IS EASIER TO TRANSFER A
Class Object Than A Struct. So Do Not Use Struct WHEN YOU ARE PASSING
Data Across The Wire Or to Other Classes.

struct is a significant type, so it is faster than the class object. Use Struct whenever you want to just store data. As a rule, the structures are suitable for programming games. However, it is easier to transfer the class object than the structure. Therefore, do not use the structure when transferring data over a network or other classes.

Answer 2, Authority 200%

This is a rather interesting question.

On the one hand, the structure in C # is. But they are very similar to classes: they also have designers – destructors, methods. Do not take care of initialization of fields of structures – .NET will not forget it.

The only difference that comes to mind – that the fields of the structure can not be initialized at the time of the announcement.

and that when creating an instance of the NEW structure is not needed, a simple word declaration

useriunfo ui;
  ui.name = "Cat";

The main difference structures from classes – in the implementation. Any instance of the class is created in the “heap” (because the class is a reference structure of data), and instances of structures are created on a stack as simple variables of primitive types.

This leads to the fact that the active creation is the release of classes instances will give the Garbage Collector work, and there are no structures. In general, the scatructures are needed where the performance in the first place, and the data is not very much.

Some advantages of the structures that manifested themselves in C / C++: it was possible to directly record the “set of bytes of long-term with the structure” into the binary file – and it was saving the structure of the structure in the file! without any serialization. And then exactly the same reading. Or – transmission over the network.

In the comments, they suggest that there are the same features in C #, but it is more difficult to use them than in C++.

If I find examples of such use – I will try to add this answer.

Answer 3, Authority 50%

is the old guide In the Microsoft documentation about when using classes, and when structures. There is a sufficient description of classes and structures, as well as the difference between them.

Where can I use structures? And in what situations it is better to resort
to structures than to classes?

Summarize answer to your question from this article:

As a rule, most types in framework developed on C # are represented by classes . However, sometimes structures will be a more appropriate type due to the fact that they relate to significant types (the main advantage is high performance with proper use).

✔️ Use structures instead of class if type instances occupy a small size in memory, and most often are short-lived or embedded in other objects.

❌ Do not use the structures if the type does not have all the following features :

  • logically represents one value similar to primitive
    types (int, double etc.);
  • instance size of this type occupies less than 16 bytes in memory;
  • it should be unchangeable (Immutable);
  • Its Do not have often pack (BOXING ).

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions