Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are ValueTypes in Object Oriented Languages like C# ,Java [closed]

Tags:

java

c#

oop

Hi I just want to know for knowledge why there are Value Types in C# or Java . Why they all are not Reference Types?. I can see two reasons for my own: 1)Performance: All commonly used DataTypes are Value types since Value Types have single round where as Reference Types have two rounds first to stack and then to heap.So performance can be the reason. 2)Assigning Values.Any help will be highly appericiated.Thanks

like image 507
yo chauhan Avatar asked Mar 01 '26 08:03

yo chauhan


1 Answers

Part of it is performance but a bigger reason is the resulting behavior. You tend to think of integers (for example) as immutable value types. However, if you're dealing with an object, you would be surprised if updating a field in that object wasn't maintained in a calling method because you were modifying a copy.

like image 175
Jeff Avatar answered Mar 03 '26 21:03

Jeff