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.