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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Priya LingeRank #785 · 2.9K reputationPosted Dec 27, 2011, 7:18 PM EST
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.
Datta KharadRank #2,135 · 668 reputationPosted Dec 27, 2011, 8:28 PM EST
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/
Gohil JayendrasinhRank #490 · 5K reputationPosted Dec 27, 2011, 9:04 PM EST
http://msdn.microsoft.com/en-us/library/t63sy5hs%28v=vs.80%29.aspx
http://www.albahari.com/valuevsreftypes.aspx