What to Know About Value Types and Reference Types in C#
page 3 of 8
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 40548/ 76

Storage Considerations

One fairly obvious statement is that data types take up different amounts of space. An integer obviously does not take as much space as a Person class would. The Person class would require space for a name, birth date, etc. We also have collection types, and with collections we do not know how much space we will need for them. It is not defined in advance at all. We need to consider and understand how each of these cases is handled.

As a general rule, we store value types on the stack in C# and we store reference types on the heap. This is not entirely correct, but it is fairly close. Value types are stored on the stack when they're within the scope of a method. This is because methods live on the stack. If the value type for example is associated with a class, it is being associated with a reference type and is therefore stored with the reference type. This means that it will be included on the heap along with the rest of the data associated with the reference type.

Stack

The stack manages the currently executing code. The information currently running is here. The currently executing code is here, which means that it contains for example method-specific information. This means local variables are stored here, which includes value types and in some sense the reference types. The reference types are here, but they only contain the address of the actual data here. This lets you have access to them in the current method even though the data is stored elsewhere. The stack builds up as we go deeper into methods. You have probably seen a "Stack Trace." It is basically some diagnostic info showing what the current information on the stack is. For the most part, this is just a list of nested methods. We call it a stack because each new context stacks on top of the previous one.

Heap

The heap is a separate section of memory whose main purpose is simply to manage the data we want to declare dynamically. The main benefit of this memory is that it is easily allocated for objects of varying size, collections usually. This is the area where the actual data is stored for variables of reference type. We have a pointer to the data which we do not see because .NET is a managed language. The pointer is the variable we are using. It is stored on the stack. It is the way we access this heap-stored data. This is why we call it a reference type. It is because we basically just keep a reference to the data.

Dynamic Data Types

Some data types require an unknown amount of space. This creates the need for memory to be dynamically allocated for this data. When we dynamically allocate data, we are allocating this data in the managed heap of C#. As I have said previously, our collections are the primary form of dynamic data in C#. Lists, stacks, queues, etc. are a few of the collections which we use. These are obviously all reference types. If you tried to store collections on the stack it would get pretty messy pretty quickly, since collections tend to grow and shrink. That's why the heap is so much nicer than the stack for dynamic data.


View Entire Article

User Comments

Title: feedback   
Name: vishal
Date: 2012-12-27 3:12:57 AM
Comment:
super awesome articles
Title: What to Know About Value Types and Reference Types in C#   
Name: Venkatesh
Date: 2012-09-26 4:02:32 AM
Comment:
hi ur explanation is very super.Figure 2: Reference Assignment
doesnt display please correct


thanks
Title: What to Know About Value Types and Reference Types in C#   
Name: sreenath
Date: 2012-03-06 12:27:36 AM
Comment:
very easy to understand doing a good job......
Title: What to Know About Value Types and Reference Types in C#   
Name: Rakesh Kumar chouhan
Date: 2011-03-03 11:47:38 PM
Comment:
thansks alot...Very Easy to understand
Title: Feedback   
Name: Sneha Bhavar
Date: 2010-08-30 1:20:15 AM
Comment:
this article is very useful one in respect of c.net platform as for beginners
Title: Compliments   
Name: Pramod Bhat
Date: 2010-02-17 9:08:20 AM
Comment:
Hey brendan,

This article is superb.especially the examples with pictures.I tried a lot to understand the diff btwn value vs reference types.But, After I read this article, now no more confusion..!!! thanks a lot
Title: RE: Need Help   
Name: Brendan Enrick
Date: 2009-11-23 1:41:48 PM
Comment:
Thanks saurabh. I am glad you found the article useful. I have written a blog post which should help explain the behavior you're seeing in your code.

http://brendan.enrick.com/blog/a-quick-answer-about-reference-types/
Title: Need Help   
Name: saurabh
Date: 2009-11-17 1:37:35 AM
Comment:
Title: Need Help
Name: saurabh
Date: 11/17/2009 1:32:18 AM
Comment:
First of all the article is excellent.
But why the following program produces output as:: abc:xyz
it should produce xyz:xyz.
I am confused...Plz help
The Code:

string myName = "abc";
string authorName = null;
authorName = myName;
authorName = "xyz";
Console.WriteLine("{0}:{1}",myName,authorName);
Console.ReadLine();
The Actual Output:
abc:xyz
The Expected Output:
xyz:xyz

as it's areference type only one copy is shared between references.
Title: Need Help   
Name: saurabh
Date: 2009-11-17 1:32:18 AM
Comment:
First of all the article is excellent.
But why the following program produces output as:: abc:xyz
it should produce zyx:zyx.
I am confused...Plz help
The Code:

string myName = "abc";
string authorName = null;
authorName = myName;
authorName = "zyx";
Console.WriteLine("{0}:{1}",myName,authorName);
Console.ReadLine();
The Actual Output:
abc:zyx
The expected Output:
xyz:xyz
Title: Best Explaination   
Name: MIten Shah
Date: 2009-11-04 11:41:38 PM
Comment:
Excellent article help me lot about understand value type and reference type
Title: Fantasic Work   
Name: Dinakran.U
Date: 2009-09-17 5:40:55 AM
Comment:
Tnx for the explantion.
Itz really gud:)
Thank u so much,Sir:)
Title: Best foundation for Value and Reference Types   
Name: Kannan
Date: 2009-08-22 6:10:06 AM
Comment:
Enrick,
This article is very useful yaar. Now I am clear with Value and ReferenceTypes...
Title: Easy and clear   
Name: Alessandro Camargo
Date: 2009-07-27 6:53:50 AM
Comment:
Excellent article easy to understand and very helpfull.

Thanks,
Title: clear understanding of value type and reference type   
Name: haritha ponnam
Date: 2009-03-03 12:44:59 AM
Comment:
hi Thanq verymuch u have given good artical it is givindg clear explanyion. now i am clear about boxing and unboxing, nullable values and much clear abt stack and heap.Thax again
Regards,
Haritha Ponnam
Title: What to Know About Value Types and Reference Types in C#   
Name: DombaX
Date: 2008-12-19 9:50:34 AM
Comment:
Excellent.. Tx...
Title: ggf   
Name: jagan nagabilli
Date: 2008-11-28 12:13:48 AM
Comment:
excellent article.....its really helps us alot..thanz author
Title: RE: Reference Assignment   
Name: Brendan Enrick
Date: 2008-11-13 10:44:30 AM
Comment:
@Horus Thanks Horus, hmm I can't believe I forgot to mention the immutability of strings. Thanks for mentioning it.
Title: Reference Assignment   
Name: Horus
Date: 2008-11-13 10:15:44 AM
Comment:
Good articel. You have to be careful with your example in the chapter 'Reference Assignment' thought. The assignment in the example works exact the way it is illustrated, but the text after the figure is only true for reference types that aren't strings. Strings are implemented as immutable and will automatically created a new instance if they are changed. So a change of 'myName' would create an independent instance with 'myName' pointing to it and authorName would keep its pointer to the old instance and so its value would remain unchanged.
Title: Value Types & Reference types   
Name: Vishal Khot
Date: 2008-10-14 4:54:15 AM
Comment:
great yaar
i am really happy by the fact that there are people who has the
tremendous capacity like you to explain the matter
it was very good & thorough explaination
great man
keep doing this
Title: Thanxx Brother   
Name: Muhammad Atif Javed
Date: 2008-09-25 2:37:18 AM
Comment:
You'r posted article is very very nice. I learned a lot from this article. This article is written in very easy words with easy to understand examples and this this will clear my concepts about value types and reference types.
Title: very helpful   
Name: Sivakumar
Date: 2008-09-10 2:55:16 AM
Comment:
Very very helpful
Good for every one.....
Title: Very very helpful   
Name: Poornima
Date: 2008-09-05 2:04:04 PM
Comment:
It is a fantastic article written in very simple language which makes it easy for beginners like me to understand.I understood everything explained in the article and Now i think I can make some sense out of the big fat C# books which I was trying to read in the first place.After struggling through complicated articles and ebooks this was like a breath of fresh air.Thanks a ton
Title: A Good Article for Beginners.   
Name: Ahsan
Date: 2008-07-09 1:19:11 AM
Comment:
It is really a very nice article for the beginners to get very Good Concept on Value Type and Reference Type.

Thanks for providing such a clear explanation.
Title: Very informative   
Name: Prince
Date: 2008-07-09 1:03:24 AM
Comment:
It was really informative. All concepts are well described except nullable which i couldn't make out :) Thanks for sharing this.
Title: Good Article   
Name: suchi banerjee
Date: 2008-07-09 12:37:57 AM
Comment:
It is a very good article. Probably you could have added a little more i.e. Cloning.
Title: RE: Good Article   
Name: Brendan
Date: 2008-07-08 8:57:35 AM
Comment:
@Ravi Chougule

Thanks, I was trying to keep it simple and easy enough for beginners to understand. I am quite happy to hear that it is also valuable for you.
Title: Good Article   
Name: Ravi Chougule
Date: 2008-07-08 5:59:37 AM
Comment:
This is really good atrticle for all...not just beginners.
Thank you






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-18 4:00:26 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search