Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Friday, 20 July 2012

Diffrence between value type and refrence type in c#.net?


Value Type:

A Value Type stores its contents in memory allocated on the stack. When you created a Value Type, a single space in memory is allocated to store the value and that variable directly holds a value. If you assign it to another variable, the value is copied directly and both variables work independently. Predefined datatypes, structures, enums are also value types, and work in the same way. Value types can be created at compile time and Stored in stack memory, because of this, Garbage collector can't access the stack.

e.g.

int x = 10;



Here the value 10 is stored in an area of memory called the stack.

Reference Type:

Reference Types are used by a reference which holds a reference (address) to the object but not the object itself. Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another doesn't copy the data. Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value. Reference Type variables are stored in a different area of memory called the heap. This means that when a reference type variable is no longer used, it can be marked for garbage collection. Examples of reference types are Classes, Objects, Arrays, Indexers, Interfaces etc.

e.g.

int[] iArray = new int[20];



In the above code the space required for the 20 integers that make up the array is allocated on the heap.




The picture shows a rough layout of a program in memory. The first four areas (Program Code, Static Data, Uninitialized Data and Stack) are fixed in size when the application is linked and the heap is what's left over.

This is for each application, but because of the way a CPU virtualizes memory, each application runs in its own space and sees itself as having access to all available ram.

The stack holds value type variables plus return addresses for functions. All numeric types, ints, floats and doubles along with enums, chars, bools and structs are value types.

The heap hold variables created dynamically- known as reference variables and mainly instances of classes or strings. These variables are stored in two places; there's a hidden pointer to the place in the heap where the data is stored.

Another distinction between value and reference type is that a value type is derived from System.ValueType while a reference type is derived from System.Object.



Data Types in .Net Value Types Reference Types
allocated on stack allocated on heap
a value type variable contains the data itself reference type variable contains the address of memory location where data is actually stored.
when you copy a value type variable to another one, the actual data is copied and each variable can be independently manipulated. when copying a reference type variable to another variable, only the memory address is copied. Both variables will still point to the same memory location, which means, if you change one variable, the value will be changed for the other variable too.
integer, float, boolean, double,struct etc are value types. string and object,class are reference types.






using System;
struct Struct1
{
    public int Value;
}
class Class1 {
    public int Value = 0;
}
class Test
{
    static void Main() {
        Struct1 v1 = new Struct1();
        Struct1 v2 = v1;
        v2.Value = 123;
        Class1 r1 = new Class1();
        Class1 r2 = r1;
        r2.Value = 123;
        Console.WriteLine("Values: {0}, {1}", v1.Value, v2.Value);
        Console.WriteLine("Refs: {0}, {1}", r1.Value, r2.Value);
     }
}
Here is the output:
c:\Windows\Microsoft.NET\Framework\v2.0.50727>ref.exe
Values: 0, 123
Refs: 123, 123


Pictorial Representation Of diffrence between value and refrence type








Pictorial Representation Of diffrence between Stack and Heap

No comments:

Post a Comment