What is the difference between Class and structure?

Started by lovtzova, Jun 20, 2022, 02:22 AM

Previous topic - Next topic

lovtzovaTopic starter

Structure used to bundle data's of different data types to perform a particular function. Extended function of the structure is the class.

The major difference is that any variable declared inside the structure is public whereas any variable declared inside the class is private, meaning the variable has meaning only inside the class.
  •  

Kovtalo

The class can create a subclass that will inherit the parent's properties and methods, whereas Structure does not support the inheritance. 
A class has all members private by default. A struct is a class where members are public by default.
  •  

Sevad

1: Classes are usually used for large amounts of data, whereas structs are usually used for smaller amounts of data.
2: Classes can be inherited whereas structures not.
3: A structure couldn't be null like a class.
4: A structure couldn't have a destructor such as a class.
5: A structure can't be abstract, a class can.
6: You cannot override any methods within a structure except the following belonging to the type object:

Equals()
GetHashCode()
GetType()
ToString()

And the other polymorphism technique used for structures is implementing interfaces.

7: Declared events within a class are automatically locked and then they are thread safe, in contrast to the structure type where events can't be locked.
  •  

arthyk

The concept of a class is fully related to object-oriented programming, with all the ensuing consequences. That is: encapsulation, inheritance and polymorphism - all these concepts refer to classes. In contrast, a structure is just a chain of named values that are numbered sequentially.
  •  

ivanka

A struct is a value type, a class is a reference type. The main difference between the two is that a struct is stored entirely on the stack whereas a class object is stored on the heap and its reference is on the stack. As a result, access to the data structure will be faster than to the class.
  •  

DiHard

what is the difference between structs and classes
Let's look at an instance that already has an error:

struct Time
{
public Time() { ... } // compile-time error
...
}
The reason for the error is that you cannot use a default (parameter less) constructor for a struct, because the compiler always generates one. As for the class, the compiler creates a default constructor only if you didn't create one. The generated constructor for a struct always sets the fields to 0, false, or null - just like for classes.
Therefore, you can be sure that the created structure will always behave "adequately" in accordance with the default values in the types used. If you don't want to use the default values, you can initialize the fields with your own values in the constructor with parameters to initialize.
However, if some value is not initialized in that constructor, the compiler will not initialize it for you and will show an error.

struct Time
{
  private int hours, minutes, seconds;
  ...
  public Time(int hh, int mm)
  {
      this.hours = hh;
      this.minutes = mm;
  } // compile-time error: seconds not initialized
}
The first rule of the Structure: All variables must always be initialized!

In classes, you can initialize the value of fields directly at the place of their declaration. This cannot be done in structures, and therefore that code will cause a compilation error:

struct Time
{
  private int hours = 0; // compile-time error
  private int minutes;
  private int seconds;
  ...
}
The second rule of the Structure: You can not initialize variables at the place of their declaration!

Using Structures as Variables
Once you have created structures, you can use them just like classes and other types. For instance , by creating a Time struct, I can use it in a class:

struct Time
{
    private int hours, minutes, seconds;
    ...
}

class example
{
    private Time currentTime;

    public void Method(Time parameter)
    {
        time localVariable;
        ...
    }
}
By the way, ...

You can create a nullable version of a struct type variable using the "?" modifier. and then set it to null:

time? currentTime = null;

Struct Initialization
In the article, I said more than once that the fields of a structure can be initialized when using a constructor, and it doesn't matter which "own" or "default" one. Another feature of structures is the fact that, due to the fact that structures are a value type, it is possible to create a structure without using a constructor, for instance :

time now;
In such a case, the variable is created, but the fields will not be initialized according to the constructor's parameters.

Use structs, it's a sign of good practice in C# programming and may the power of... structs be with you. Quote from the comment: "I also disagree with the conclusion.
Many venerable Western authors, on the contrary, recommend using structures as little as possible, preferring classes. Although, of course, you shouldn't pray for them and their opinion, it's better to think with your own head."

  •