Why int is a struct in C#? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

8th Jan 2017, 9:43 AM
/dev/null
/dev/null - avatar
3 Answers
+ 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.
9th Jan 2017, 10:24 AM
Andres G
Andres G - avatar
+ 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.
8th Jan 2017, 10:33 AM
Sanyam Kumar
Sanyam Kumar - avatar
0
@Andeas, I liked your answer. It sounds logical and convincing. Thanks
9th Jan 2017, 10:54 AM
/dev/null
/dev/null - avatar