When should you use a structure?

If I have a class and it’s essentially just a bunch of variables - it has no methods, actually more storage space - is it better to convert it to a structure?

What is a β€œrule” when to use a construct and when to use a class?

+4
source share
6 answers

Rule number one is that it should not exceed 16 bytes. Usually 4 fields. The generated jitter code takes an unpleasant peak when it becomes larger than this. This is incompatible with a bunch of variables. There is nothing wrong with a simple class.

+2
source

My rule: when an object represents a value (and therefore immutable) and does not have an identifier (for example, two instances of "5" are the same). Therefore, you must override == as well as .Equals. String , for example, is a class only for pragmatic reasons. Similarly, do not use the structure for more than a few fields. Perhaps it can be further decomposed.

+1
source

- the member and class inheritance is private by default, while they are public in the structure.

-Use a class when the object identifier is more important than the value. Use struct when the value contained in an instance is more important than the instance identifier.

-structures are types of values; classes are reference types.

-A structure must have at least one non-distributed variable or event element. the class may be completely empty.

- if you need to handle an event use class.

-classing heap allocation when using stack on stack

-if you need to initialize any member value and then use the class because you cannot do this on the stack.

I think now you can understand the differences. You can now use the stack or class to suit your needs.

thanks Arefin

+1
source

From Effective C #

Value types or reference types? Structures or classes? When should you use each? This is not C ++, in which you define all types as value types and can create references to them. This isnt Java, in which everything is a reference type (unless you are one of the language developers). You must decide how all instances of your type will behave when you create it. This is an important decision for the first time. You have to live with the consequences of your decision, because changing later can lead to code to break the subtle paths. this is a simple question about choosing a structure or a class keyword when creating a type, but its much more updated for all clients using your type if you change it later.

It is not as simple as preference on the other. The right choice depends on how you expect to use the new type. Value types are not polymorphic. They are better suited for storing data that your application is manipulating. Link types can be polymorphic and should be used to determine the behavior of your expression. Consider the expected responsibility of your new type and from these responsibilities, which type to create. Store data structures. Classes define behavior.

From C # in depth

Suppose you read something fantastic and want a friend to read it too. Suppose further that his document is in the public domain, simply to avoid any allegations of supporting copyright infringement. What do you need to give your friend so that he can read it too? It depends entirely on what you read.

First, let's take a good look at the case when you have real paper in your hands. to give a friend a copy, you need to copy all the pages, and then give it to him. At this moment, he has his own full copy of the document. This situation dealt with value type behavior. All information is right at your fingertips - you do not need to go anywhere else to get it. Your copy information is also independent of your friends after you made a copy. You can add some notes to your pages, and its pages will not be changed at all.

Compare this to the situation when you read a web page. This time, all you need is to give your friend the URL of the web page. This is a link type behavior, with a URL replacing the link location. In order to really read the document, you need to follow the link placing the URL in your browser and asking him to load the page. On the other hand, when you change the web page for some reason (imagine your wiki and you added your notes to the page), you and your friend will see that the next time each of you loads the page.

The differences we saw in the real world is the heart of the difference between value types and reference types in C # and .NET. Most types in .NET are reference types and you are likely to create much more than value types. Most common cases that you should know are classes (declared using a class) are reference types and structures (using struct is declared) - value types. Other following situations:

Array types are reference types, even if the element type is a value type (therefore int [] is still a reference type, even if int is a value type).

Enumerations (declared using an enumeration) are value types.

Delegate types (declared using a delegate) are reference types.

Interface types (declared using an interface) are reference types, but they can be implemented by value types.

Now that we have a basic understanding of what reference types and value types are about, take a good look at some of the most important details.

+1
source

See ... this msdn article

Structures versus classes

Structures may seem like classes, but there are important differences that you should be aware of. The first of all classes are reference types and structs are value types. Using structs, you can create objects that behave as built-in types and also take advantage of them.

Heap or stack?

When you call the New operator on a class, it will be allocated to the heap. However, when you create an instance of struct, it is created on the stack. This will give a performance boost. In addition, you will not deal with a reference to an instance of a structure as well as with classes. You will be working directly with the example structure. Because of this, when passing a structure to a method, it is passed by value, and not as a reference.

So, if you use data only in the method and do not pass it to functions, then the structure can give you a performance boost, since it is allocated only on the stack ... if you are going to pass it a bunch of functions, then the class is probably better, because it will be transmitted by reference, and not "copied" again and again ...

0
source

Structures are usually used for small amounts of data, which are a single cohesive unit of information. They are usually passed by value, which implies the transfer of a copy of it, rather than a link to the object itself.

This means that the structures have a couple of minor errors. If

  • the data will have semantics of values ​​- that is, if the object can be cloned without problems, and the values ​​are important, not their identifiers;
  • the total amount of data will be less than about 16 bytes, since the execution time will end with copying, and not just passing a link; and
  • if it is unchanged, since changing the copy will usually not change the original;

then you may need to use struct.

If any of them is incorrect, you may want to save the class as a class.

0
source

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


All Articles