+ 1
Why int is a struct in C#?
int is a struct in C#, but why is not defined as class such as strings? Having it as class rather than struct might introduce more benefits to it. what do u think?
3 Respuestas
+ 2
If int were a class, this would happen:
int a = 3;
int b = a;
a = 5;
Console.Writw (b);
\\ Outputs 5.
A struct is a value type that means that each object has a its vaule storage in the stack.
A class has its reference (direction of where the object is saved on the heap) storage on the stack.
When you equate two variables you give the value of one to the other. When you equate to objects you give the reference of one to the other, so if one change the other will aswell.
+ 1
int is a value type, and string is a reference type. Value types are passed by value (copied) when you pass them to a method or assign them to a new variable and so on. Reference types only have the reference copied.
0
@Andeas, I liked your answer. It sounds logical and convincing. Thanks