Here
I will explain what is Value Type v/s Reference Type and uses of Value
Type v/s Reference Type with example in asp.net using c# .
Description:-
In previous posts I
explained Asp.net View State, What is the Dataset in ASP.NET, Difference between .NET
Framework 4.5 and .NET Framework 4.0, What is Cloud Computing, Basics of ASP.NET . Now I will explain what is Value Type v/s Reference
Type and uses of Value Type v/s Reference Type with example in
asp.net using c#
What is Value Type and Reference Type in C#
Unlike
some programming languages you might be familiar with, C# has two varieties of
data types: value and reference. It's important to know the difference if
performance is important to your application, or if you are interested in how C# manages data and memory.
Before diving into the deep sea of
C#, I would like to touch the basics of reference and value types.
Reference Types: Always allocated from the managed heap.
Always represented in boxed form.
When we assign a value type to another value type, a field-by-field copy is made.
Value Types: Allocated on thread's stack.
Have two form representations "boxed" and "unboxed"
When we copy a reference type to another reference type, only the memory address is copied.
Reference Types: Always allocated from the managed heap.
Always represented in boxed form.
When we assign a value type to another value type, a field-by-field copy is made.
Value Types: Allocated on thread's stack.
Have two form representations "boxed" and "unboxed"
When we copy a reference type to another reference type, only the memory address is copied.
When
a variable is declared using one of the basic, built-in data types or a user
defined structure, it is a value type. An exception is the string data type, which is a reference type.
A
value type stores its contents in memory allocated on the stack. For example,
in this case the value 30 is stored in an area of memory called the stack.
int x = 30;
When
the variable x goes out of scope because the method in
which it was defined has finished executing, the value is discarded from the
stack.
Using
the stack is efficient, but the limited lifetime of value types makes them less
suited for sharing data between different classes.
In
contrast, a reference type, such as an instance of a class or an array, is
allocated in a different area of memory called the heap. In the example below,
the space required for the ten integers that make up the array is allocated on
the heap.
int[] numbers = new int[10];
This
memory isn't returned to the heap when a method finishes; it's only reclaimed
when C#'s garbage collection system determines it is no longer needed. There is
a greater overhead in declaring reference types, but they have the advantage of
being accessible from other classes.
Reference
types are typically declared using "class" keyword, while the value
types are declared by the keyword "struct". Let's see an example of
each.
public class ReferenceType
{
public int Field { get; set; }
}
public struct ValueType
{
public int Field { get; set; }
}
Let's now create a console application :-
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Reference
Type");
ReferenceType referenceType1 = new
ReferenceType();
ReferenceType referenceType2 = new
ReferenceType();
Console.WriteLine(referenceType1.Equals(referenceType2));
Console.WriteLine("Value
Type");
ValueType valueType1 = new
ValueType();
ValueType valueType2 = new
ValueType();
Console.WriteLine(valueType1.Equals(valueType2));
Console.ReadLine();
}
}
You
might have expected that the first Console.WriteLine statement would print
"true", but the actual result is "false". System.Object is
considered the parent type of all other types and it has the method Equals and
implements it in a way that doesn't care about the object's fields, but
rather on its reference. However, all value types inherit from
System.ValueType, which itself inherit from System.Object. System.ValueType
overrides the implementation of Equals with a more logical one that depends on
the values of object's fields.
Let's now have another example and
see how reference types are considered equal.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Reference
Type");
ReferenceType referenceType1 = new
ReferenceType();
ReferenceType referenceType2 = referenceType1;
Console.WriteLine(referenceType1.Equals(referenceType2));
Console.WriteLine("Value
Type");
ValueType valueType1 = new
ValueType();
ValueType valueType2 = valueType1;
Console.WriteLine(valueType1.Equals(valueType2));
Console.ReadLine();
}
}
Here,
both of them print "true". The second Console.WriteLine statement
obviously prints true because both objects have the save values for their
fields. For the first statement, this explains to us the fact that reference
types are used by their reference. In other words, when assigning
"referenceType1" to "referenceType2", the CLR only copies
referenceType1's reference and assigns it to referenceType2 so both of them
become pointing to the same object data in the memory.
Let's summarize and add some other
differences between Value Type and Reference Type in the following table.
Value Type | Reference Type | |
where are they stored in memory ? | Stack | Heap |
Parent Type | System.ValueType | System.Object |
Inheritance | All Value Types are sealed and you can't inherit from other types | You can normally inherit from other types |
Acess modifiers | Protected can not be use | Any Access modifier can be used |