Hi,
What is reference type? What is difference between value type and reference type?
Thanks.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Gohil JayendrasinhPosted Dec 28, 2011, 2:04 AM
http://msdn.microsoft.com/en-us/library/t63sy5hs%28v=vs.80%29.aspx
http://www.albahari.com/valuevsreftypes.aspx
Datta KharadPosted Dec 28, 2011, 1:28 AM
Refer these articles..
http://www.c-sharpcorner.com/UploadFile/jaishmathews/WorkingofReferenceTypeandValueType09102006022502AM/WorkingofReferenceTypeandValueType.aspx
http://www.c-sharpcorner.com/uploadfile/gowth/difference-between-value-type-and-reference-type/
Priya LingePosted Dec 28, 2011, 12:18 AM
1.All value based types are allocated on the stack.
2.All reference based types are allocated on the heap.
3.A value type contains the actual value.
4.A reference type contains a reference to the value.
5.When a value type is assigned to another value type, it is copied. When a reference type is
assigned to another reference type, a reference is assigned to the value.
6.All value types are implicitly derived from System.ValueType. This class actually overrides the implementation in System.Object,
the base class for all objects which is a reference type itself.
7.Numeric data types such as integers, floats, etc, boolean, enumerations and user defined structures are value types.
8.Classes, interfaces and delegates are reference types.
Object, string, dynamic are the built in reference types.
9.Example :
using System;
using System.Collections.Generic;
using System.Text;
namespace CSApp1
{
class Shape
{
public int length;
public int width;
public Shape()
{
length = 0;
width = 0;
}
}
// Make struct Rectangle to solve the Null reference exception problem
class Rectangle
{
public Shape MyShape;
}
class MyClass
{
static void Main(string[] args)
{
Rectangle obj1;
obj1.MyShape.length = 100; // will cause System.NullReferenceException
System.Console.WriteLine(obj1.MyShape.length);
}
}
}
Hope this will help you.Thanks.