Value Type Vs Reference Type

There is always a confusion between value types vs reference types or a struct vs a class. Specially during interview times .. :), when Interviewer starts question by asking whats the difference between Value Types vs Reference Types and ends by delocating of memory.
 
There is a simple difference of these two is about how these are allocated in memory.
e.g.
 
Value Types like struct, int, double etc. all premitive data types are stored in Stack and Reference types like classes are stored in Heap.
In classes, there are two things : one is instance and second is object.
Class Employee
{
int ID;
int Salary;
}

Employee Emp; // this is instance
Emp = new Employee;//this is object

So, Instance is stored in Stack, whereas actual object is stored in Heap.

Memory Allocation / De-Allocation:

In case of Value types variables, as the value is stored in Stack, after function execution, all values from Stacks are popped and memory deallocated.   In case of Reference types, as references are stored in Stack, these are also popped up from Stack but the main object which is created in Heap, that still remains in memory until Garbage Collector cycle doesn’t run because it’s GC(Garbage Collector) responsibility to clear all unallocated objects.

Passing as Parameter:

Suppose there is a struct say Point P and Class Employee as defined above: 

Struct Point{int X;int Y;} 

Class Employee{int ID;int Salary;}

public void ChangeValues(Point P, Employee emp)
{
P.X = 200; //There will be no change on original P.X passed
emp.ID = 5; //This will change original ID
emp = null; //No change on original Emp object
}

Reason : Point is a value type which is passed as copy of original value, so any change done inside the function will not reflect original value. Employee emp has been passed as a reference(instance), So emp represent here as a pointer to main object, so emp = null will not change the oringinal object, but this emp reference points to original object, so emp.ID will change original object properties.

Let’s clear the picture by passing these two as passing by reference.

public void ChangeValuebyRefernce(ref Point P, ref Employee emp)
{
P.X = 200; //There will be change because refernce to original value is passed
emp.ID = 5; //This will change original ID
}

This was the brief summary of how value types and refernce types ate stored in memory, how these are deallocated from memory, how these behave in passing as parameter. In further blogs, I’ll try to clear more doubts.

Out Vs Ref

There are some basic differences in Out and Ref.

Out :  Passed by reference and it must be set by the called method, otherwise there will be compiler error.

e.g.

int x; // no need to assign any value as the method in which it will be passed as OUT , its method responsibility to set its value.

void OutEx(out int x)
{
//x should be set some value before exiting from this method other compiler error
}

Ref : Already assigned passed and no need to reset in the called method.

e.g.

int x = 0;//here x must be set before passing as ref, because its not guarantee that the method in which it is passed as Ref, will set.

void RefEx(ref int x)
{
// x is already set, so no need to set. but you change
}

Another benefit of out modifier :  It allows the caller to obtain multiple return values from a single method invocation.

Interviewer can ask in a different way like : If you have to return multiple values from a single method, what will you do. Simple answer is use Out modifier.