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

Simple Conceptual Difference

I will start by giving a short and simple explanation of the differences between these two. Data types in C# will either be value types or reference types. You can think of Value types as actually being the "value." All we are storing is that value. Think of Reference types as being a "reference" to the value. In this case we are storing a reference to the value you are using.

Value Type Examples

·         int

·         float

·         bool

·         byte

Reference Type Examples

·         string

·         object

·         List

·         Regex

Notice that the simpler, baser types tend to be values and the more advanced types tend to be references. If it really seems like what is being stored is just one single value, it is likely what you are dealing with is a value type. If the data seems more complex or there are simply more bits and pieces of information which are stored together, it is likely you are dealing with a reference type.

Value types are often able to be set to literal values. (42, 3.14159, true, false)

Reference types are similar to pointers in C and C++. (They are not the same, but they are close.) I say this because it helps people understand reference types to think of them as being like pointers to the data. Some C++ purists will probably criticize me for explaining it this way.

Figure 1: Basic Concept

In Figure 1, I have defined two variables: foo and bar. Notice that the value of foo is stored at the location of foo, but with bar there is only a reference to the letters.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 5 and 2 and type the answer here:

User Comments

Title: ggf   
Name: jagan nagabilli
Date: 11/28/2008 12:13:48 AM
Comment:
excellent article.....its really helps us alot..thanz author
Title: RE: Reference Assignment   
Name: Brendan Enrick
Date: 11/13/2008 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: 11/13/2008 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: 10/14/2008 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: 9/25/2008 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: 9/10/2008 2:55:16 AM
Comment:
Very very helpful
Good for every one.....
Title: Very very helpful   
Name: Poornima
Date: 9/5/2008 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: 7/9/2008 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: 7/9/2008 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: 7/9/2008 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: 7/8/2008 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: 7/8/2008 5:59:37 AM
Comment:
This is really good atrticle for all...not just beginners.
Thank you

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






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


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 12/2/2008 9:06:21 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search