Saving Reference Types in Struct

Say I had a very simple structure in C #

public struct foo{ public int a{get;set;} public int b{get;set;} public int c{get;set;} public int d{get;set;} public string hello {get;set;} } 

I believe the above is more efficient than using class ?

At what point, if I continued to add string properties, would there be time to convert the structure to a class?

Edit: I planned to pass this structure around a GDI-oriented application. I have always believed that structures are more efficient when working with basic value types.

+4
source share
6 answers

If you need the advanced features that the class offers, such as inheritance, then switch. If not, the structure may be a little β€œlighter”, but if you do not expect some performance problems, such as garbage collection inside a hard loop with a lot of iterations, the need to pass structures using ref whenever you want a change method, etc. .d. may create unnecessary work. (Although in this example, destroying a structure that has properties of a reference type also calls GC).

Bottom line: using a structure or class is a matter of your use, not the number of properties you have.

For a good explanation of the differences between relative strong and weak classes and structures, see this MSDN article .

Eric Lippert has a great post on garbage collection, structures and classes, see his answer to this question .

+4
source

I find that higher is more efficient than using a class?

Absolutely not. This is a terrible structure . Structures should be small; no more than, say, four times the magnitude of the link; your structure has a size of 5 links on a 32-bit machine. Structures must represent values; this does not seem to make any difference. Structures must be unchanged; it is a push full of volatility.

At what point, if I continued to add string properties, would there be time to convert the structure to a class?

The point of conversion of this structure to a class was the moment you created it. Firstly, it was never a structure. Your default assumption should be that the classes are good enough; just move on to the structure when you have evidence that this solves the problem that you have.

I planned to pass this structure around a GDI-oriented application. I have always believed that structures are more efficient when working with basic value types.

Structures are more efficient than classes in a very small number of cases: when they are small, immutable, represent values ​​and consist of other values, such as integers. In other cases, they are less efficient: since structures are copied by value, large structures can be much slower to use than links. Since structures are copied by value, volatile structures lead to errors because you mutate copies when you consider yourself mutating variables. Because structures are copied by value, they should have value semantics, not references. And since structures containing only other structures can be completely skipped by the garbage collector, structures are only more efficient for cleaning purposes when they do not contain references.

In any case, as you say, if something is more efficient, try to do it both ways and measure its performance compared to your performance goals. Use science . Set a goal and measure it. The use of structures, "since I assume they are more efficient," makes rumor-based technical decisions. Measure and then you find out which is more efficient.

+25
source

Structures have some unique strengths, but efficiency is not necessarily one of them.

As a rule, it is best to use a class if you really do not know why a structure is needed, no matter how many properties a type has.

+1
source

I do not think this is the case if you continue to add lines, this is the case of what you want to do with the object. Structures are value types and classes are reference types, there are some improvements in the performance of structures from what I understand (in terms of memory / stack heap allocation), but I think it ultimately depends on what you do with the object.

I THINK I read as soon as structures are great for short objects from them, but I could be wrong.

+1
source

If someone says:

  foo x [100];
   foo [1] = foo [2];
   foo [1] .a + = 5;

if the last statement affects foo[2].a ? If foo is a structure type, it will not; if it is a class type, it will be.

Contrary to what Mr. Lippert says, your type can be as beautiful as a structure, with a few warnings:

  • Structures should open their fields directly, and not through properties, unless the properties have some kind of nontrivial logic in them.
  • Passing over structs that are larger than 16 bytes in size is a fair bit slower than passing smaller ones, because .net has special processing for small structures. However, when passing through ref, the size of the structure is not affected.
  • Variable structures that contain mutable reference types often exhibit semantics, which are a strange crossroads between mutable value semantics and mutable reference semantics. This is not a problem for structures that contain immutable reference types, such as `string`.
  • Structure type properties are often a pain to work with. Link-type read-only link properties are sometimes easier to work with, but replacing a structure type containing 20 bytes of data with a class that also stores 20 bytes of data can almost double the storage requirements on 32-bit systems or more than double 64- bit systems.

Semantics of multiple meanings are useful. Old C # compilers (years ago) had problems with them, and some people want to prevent people from learning to understand them, but these are not good reasons to refrain from using them where necessary.

0
source

Remember that what you declare in the structure is publicly defualt. Instead, you should use a class if you want your data to be hidden. In addition, all typical OOP functions are not available in the structure.

FYI: Class (computer programming)

-3
source

Source: https://habr.com/ru/post/1396873/


All Articles