Why class is reference type?
Why class is reference type?
In C#, arguments can be passed to parameters either by value or by reference. Remember that C# types can be either reference types ( class ) or value types ( struct ): Pass by value means passing a copy of the variable to the method. Pass by reference means passing access to the variable to the method.
Because of a object created from a class is stored on a area in the memory called heap with only a reference to it on the stack memory.
When you write a method which is complext and need to split into muliple sub methods for readabilty purpose, you may have to pass the arugment by ‘Pass by reference’ to retain its functionlity.
Let’s assume that class is a value type data. If so , when we instantiate it, some amount of memory would have been allocated in Stack. It would have been difficult for Garbage collector to identify non-referenced objects and hence leading to inefficient memory management.
Since class is a reference type, GC handles memory management efficiently.
This is just my opinion. There could be some other reason as well that i am not aware of now.