Hello All,
Welcome to C Sharp Programming.
I am Chakrapani Upadhyaya a full stack engineer.
In this article we will understand the concepts like what is Value Types and Reference Types in C#
Lets Discuss and understand the concept.
Value Types:
The data types which store the data directly into their memory locations as know as value type.
Note that in value type memory is allocated at the time of compilation, Memory allocation made within the STACK(Contiguous Memory Location)
CLR will not provide automatic memory management to Value Types.
Best Example for Value types is Structs and Enumerations
Reference Types:
The data type which do not store the data directly into their memory locations rather refers to other memory locations is Known as Reference Types.
Memory is allocation at Run time
CLR Provides automatic memory management to Reference Types
Example for Reference Types in Class, Interface and Delegate
Note That:
When a value type is passed into a method as a parameter, if the method changes the value in any way, the value is not changed on top variable, But in Reference Types When we pass a reference type into that method and changing it will change the underlying object, so that other things that use that same object will have the newly changed object rather than their original value.
But What if you want to change them, i mean value types?
Simply pass them into your method using the "ref" keyword, and you are then passing this object by reference. Meaning,
it's the same object in memory. So modifications you make will be respected. See the example on passing by reference for an example.