What are the struct classes for in c #?

2

What are the struct classes in c # for, since I have seen the following:

A class inPFile

 public class inPFile
 {
     public int P_Id { get; set; }
     public string P_Name { get; set; }
     public string P_Nick { get; set; }
 }

And another class outPFile

public  class outPFile
{
    public struct PFileNodes
    {
        public int P_Id { get; set; }
        public string P_Name{ get; set; }
        public string P_Nick { get; set; }           
    }
}

Does this improve performance, or does it improve? Is it good practice?

    
asked by MC BAMBAM 08.06.2018 в 05:44
source

3 answers

5

Struct are not classes, they are structures. A structure is a logical grouping and has several properties similar to a class, but it does not have the same characteristics.

The general idea of a struct is to have a logical type of data self-contained. The most common examples would be a point (which already comes as a struct in the framework) or a color. Or you could even contain all the information of a file inside a structure.

Keep in mind that a struct is very efficient in many cases, since the trace in memory is much smaller. Likewise, a struct is defined as a type by value, therefore any copy of it creates a new structure.

There are several rules for creating structs:

  • They can not contain builders without parameters.
  • Its properties can not be initialized by default. (except some cases)
  • Instanced without using a new.
  • They can not inherit from another nor implement an interface
  • Other rules (see the documentation here )

They may contain, among other things:

  • builders
  • constants
  • fields
  • methods
  • events
  • And more (see here )

An example could be:

Public struct Archivo
{
    public int Tamaño;
    public string Nombre;
    public date FechaCreacion;
    public bool SoloLectura;
}

Keep in mind, that nothing better than checking the documentation always.

And to your question of whether it is a good practice, it depends on how you use it. Make it a good practice, it depends on you using it in the right place and at the right time. Many times, a class is wasting a lot of space. Above all, if the class has nothing but fields.

    
answered by 08.06.2018 в 07:02
2

I understand that your question is summarized in " when should I choose a structure and when a class?"

In general it is a subjective decision, but according to Microsoft, a structure must be chosen when the following conditions are met:

  

Consider defining a structure instead of a class if instances of the type are small and usually of short duration or are usually embedded in other objects.

     

Avoid defining a struct unless the type has all the following characteristics:

     
  • Logically represents a single value, similar to primitive types (int, double, etc.).

  •   
  • It has an instance size of less than 16 bytes.

  •   
  • It's immutable.

  •   
  • You will not have to do the boxing conversion frequently.

  •   

Source

But even Microsoft "violates" these rules if necessary. With what, as I say, it is a personal decision to choose one or the other, the main difference for me is that a structure is a type for value and a class is a type for reference, and that is what you can opt for one or the other . There is no strict rule on when to use one or the other ...

    
answered by 08.06.2018 в 12:07
-2

A structure would be very similar to a static class that does not need to create an instance, or use new, as long as they are only fields.

However, if something more complex is required, such as methods for example, a class is the option, whether static or not.

    
answered by 08.06.2018 в 15:21