+ 3
How do classes and structures differ?
I was going through a C # code battle and there was a question. https://code.sololearn.com/czsKtO9oqzxH/?ref=app https://code.sololearn.com/cOFgllbNO2k2/?ref=app (c = class, s = struct) Why if s2.X = 20 s1.X == 0? If c2.X == c1.X.
2 Réponses
+ 2
Class is a reference type and structures are a value type.
When you create a structure, the variable to which the structure is assigned contains the actual data of that structure.
If the structure is assigned to a new variable, all data is copied.
Thus, the new variable and the original variable contain two separate copies of the same data.
Changes made to one copy do not affect the other.
+ 1
Vasiliy , Are you saying that a value type copies the value itself, and a reference type is a reference? Then it's clear, thanks :)