BizTalk Blog by Chris Han

System Design for Enterprise Agility,

  Home  |   Contact  |   Syndication    |   Login
  66 Posts | 9 Stories | 122 Comments | 79 Trackbacks

News

Article Categories

Archives

Post Categories

Image Galleries

BizTalk Bloggers

BizTalk on MSDN

Patterns & Architecture

SharePoint

David Chappell in his article “Understanding .NET's Common Type System“ explains “A basic difference between value types and reference types is that an instance of a value type has its value allocated on the stack, while an instance of a reference type has only a reference to its actual value allocated on the stack. The value itself is allocated on the heap“

http://www.awprofessional.com/articles/article.asp?p=24456&redir=1

This is the best comment I've ever read. I'd like to keep it here for myself.

posted on Wednesday, November 09, 2005 3:02 PM

Feedback

# Where are value types and reference types allocated in memory? 11/11/2005 9:14 AM .NET and other stuff
Seems that a lot of people (including me) get this wrong the first time
around. In several books/on-line...

# re: A basic difference between value types and reference types in .NET's Common Type System 12/16/2006 4:35 AM vakrat anvesh
hi,its not good.Plz xplain it clear by this EOW

# re: A basic difference between value types and reference types in .NET's Common Type System 2/27/2007 4:12 AM Sahil
Dude, you should put some meaningfull link here. The link you have provided is about CTS, and the title you have given here is "difference between value type and ref. type."

Are we supposed to do a SEARCH through the article to find out the difference between value and ref type?

Please be specific while such posting.


# re: A basic difference between value types and reference types in .NET's Common Type System 5/1/2007 12:19 AM sarita Mohapatra
I could not got it plz explain briefly .

# re: A basic difference between value types and reference types in .NET's Common Type System 5/17/2007 3:50 AM Naveen
I am not sure that it is complete defination.


# re: A basic difference between value types and reference types in .NET's Common Type System 6/3/2007 10:50 AM sooraj
the basic diff between two would be
value type -any changes made wuld not affect the value ouside the loop or fuction
ref type-changes are made at address of the variable direclty
in .net u can see whn u pass a variabele the diff wuld be
(ByVal a as string)
(ByRef a as integer)

any correction do let me know.I would be pleased

# re: A basic difference between value types and reference types in .NET's Common Type System 1/22/2008 12:39 AM sweety
A variable that is a value type, stores the data, while a variable of a reference type stores a reference to the data.



# re: A basic difference between value types and reference types in .NET's Common Type System 1/31/2008 4:48 AM Akanksha Chauhan
Pl. explain it clearly, it is not clear to me....

# re: A basic difference between value types and reference types in .NET's Common Type System 3/14/2008 1:34 AM Sanket Hamdapure
can you plz explain in detailed with some more points...

# re: A basic difference between value types and reference types in .NET's Common Type System 3/19/2008 1:49 AM ad
newl hi

# re: A basic difference between value types and reference types in .NET's Common Type System 4/14/2008 2:41 PM Muthu
value types are inherited from system.valuetypes while the reference types are inherited directly from the system.object.

value types are stores in a stack while the later will store in amanaged heap

value types are light and the later is heavy objects

# re: A basic difference between value types and reference types in .NET's Common Type System 5/11/2009 6:45 AM kala
Value type: Means, It's stores the actual value into the variable.
Ex: Int type, float type, decimal,enumeratios.

Reference type:Means, actual reference will be stored into the variable.
Ex: Class is a reference type. Any class is a reference type, interface is a reference type.

If we declare a class as below..

Public class vehicle
->here vehicle is base class, here we will declare an instance of vehicle class.
vehicle cls = new vehicle()
Here cls is reference value which is also referred as object.

and also 'string' is also reference type.

# re: A basic difference between value types and reference types in .NET's Common Type System 8/14/2009 2:25 AM Purish Dwivedi
A variable that is a value type, stores the data, while a variable of a reference type stores a reference to the data.
Value types are stored directly on the stack while Reference types are stored on the run-time heap.


# re: A basic difference between value types and reference types in .NET's Common Type System 9/10/2009 12:38 AM Tanu Gaur
value types are stored directly on STACK.
while
Reference type are stored on a run-time HEAP.

# re: A basic difference between value types and reference types in .NET's Common Type System 10/6/2009 12:11 AM Ankit Srivastava
A variable is value type or reference type is solely determined by its data type.
Eg: int, float, char, decimal, bool, decimal, struct, etc are value types, while object type such as class, String, Array, etc are reference type.


Value Type

1) As name suggest Value Type stores “value” directly.
2) For eg:
//I and J are both of type int
I = 20;
J = I;
int is a value type, which means that the above statements will results in two locations in memory.
3) For each instance of value type separate memory is allocated.
4) Stored in a Stack.
5) It Provides Quick Access, because of value located on stack.



Reference Type

1) As name suggest Reference Type stores “reference” to the value.(i.e. Address of the variable where the value is stored.)
2) For eg:
Vector X, Y; //Object is defined. (No memory is allocated.)
X = new Vector(); //Memory is allocated to Object. //(new is responsible for allocating memory.)
X.value = 30; //Initialising value field in a vector class.
Y = X; //Both X and Y points to same memory location. //No memory is created for Y.
Console.writeline(Y.value); //displays 30, as both points to same memory
Y.value = 50;
Console.writeline(X.value); //displays 50.
Note: If a variable is reference it is possible to indicate that it does not refer to any object by setting its value to null;
3) Reference type are stored on Heap.
4) It provides comparatively slower access, as value located on heap.


# re: A basic difference between value types and reference types in .NET's Common Type System 11/10/2009 12:50 PM Bharat
What if you do something like:

int intVal = new int();

intVal is a value type. So should be created on stack. But here you are using new to allocate intVal.
What happens here?
Is intVal created on stack?
Is intVal created on managed heap?

# re: A basic difference between value types and reference types in .NET's Common Type System 11/10/2009 5:23 PM chris
That's an interesting question, Bharat. As far as I remembered that 'every class in c# is derived from object type'. Doesn't it mean value type like int should also be treated like an object? Frankly, I don't know. You let me know if find out.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: