Register now or log in to join your professional community.
Value type:
A Value Type holds the data within its own memory allocation,
Value Type variables are stored in the stack.
Reference type:
Reference Type contains a pointer to another memory location that holds the real data. Reference Type variables are stored in the heap
value means it gives that particular variable value
Reference means it gives that particular variable address
Eg : int a=10
cout<<a; //10
cout<<&a; // address of a
general variable declaration like int no=123; is value type.it simply means storing value in varible it called as value type.instead of storing a value storing address to pointing data.its called reference type.for ex. String class,array.actually value type store on stack memory and reference type store on heap memory area
Value types are stored on stack memory, where as reference types are stored on heap.
Reference Type variables are stored in the heap while Value Type variables are stored in the stack
Value type variable is holding its value and reference type means that variable is holding a pointer (address) to that object.
Main thing to care about is taking a copy, so if we say x=y
Value type:
we will have to different variables x and y holding the same value and you can modify any one of them without affecting the other.
Reference type:
we will have two variables x and y holding the same pointer address which points to one single object and if you modify any one of them you will also modify the other one
runtime deals with the two in different ways. When a value-type instance is created, a single space in memory is allocated to store the value. Primitive types such as int, float, bool and char are also value types, and work in the same way. When the runtime deals with a value type, it's dealing directly with its underlying data and this can be very efficient, particularly with primitive types.
With reference types, however, an object is created in memory, and then handled through a separate reference—rather like a pointer. Suppose Point is a struct, and Form is a class. We can instantiate each as follows:
Point p1 = new Point(); // Point is a *struct*Form f1 = new Form(); // Form is a *class*
In the first case, one space in memory is allocated for p1, wheras in the second case, two spaces are allocated: one for a Form object and another for its reference (f1). It's clearer when we go about it the long way:
Form f1; // Allocate the referencef1 = new Form(); // Allocate the object
If we copy the objects to new variables:
Point p2 = p1;Form f2 = f1;
p2, being a struct, becomes an independent copy of p1, with its own separate fields. But in the case of f2, all we’ve copied is a reference, with the result that both f1 and f2 point to the same object.
This is of particular interest when passing parameters to methods. In C#, parameters are (by default) passed by value, meaning that they are implicitly copied when passed to the method. For value-type parameters, this means physically copying the instance (in the same way p2 was copied), while for reference-types it means copying a reference (in the same way f2 was copied). Here is an example:
Point myPoint = new Point (0, 0); // a new value-type variableForm myForm = new Form(); // a new reference-type variable
Test (myPoint, myForm); // Test is a method defined below
void Test (Point p, Form f)
{ p.X = 100; // No effect on MyPoint since p is a copy f.Text = "Hello, World!"; // This will change myForm’s caption since // myForm and f point to the same object f = null; // No effect on myForm}
Assigning null to f has no effect because f is a copy of a reference, and we’ve only erased the copy.
We can change the way parameters are marshalled with the ref modifier. When passing by “reference”, the method interacts directly with the caller’s arguments. In the example below, you can think of the parameters p and f being replaced by myPoint and myForm:
Point myPoint = new Point (0, 0); // a new value-type variableForm myForm = new Form(); // a new reference-type variable
Test (ref myPoint, ref myForm); // pass myPoint and myForm by reference
void Test (ref Point p, ref Form f)
{ p.X = 100; // This will change myPoint’s position f.Text = “Hello, World!”; // This will change MyForm’s caption f = null; // This will nuke the myForm variable!}
In this case, assigning null to f also makes myForm null, because this time we’re dealing with the original reference variable and not a copy of it.
The Types in .NET Framework are either treated by Value Type or by Reference Type. A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data. Reference Type variables are stored in the heap while Value Type variables are stored in the stack.
In simple term:
Value Type = Stored in stack
Reference Type = Stored in heap
Value Type:
the variable and its data (value) are saved in memory Stack.
Ex.: all primitive Datatype.
Reference Type:
the variable is saved in memory Stack and has a Reference to its value on memory Heap.
Ex.: arrays, objects, strings.
Value Types:- Variables that store data are called value types. Value types are stored on stack.
They contain the actual values. eg-int, enum, structs.
Reference Types:- Variables that store reference to actual data are called Reference types.Reference types stored on heap but contain the address on heap.
eg-class,interface,delegate,string,object, Array