Value Types and Reference Types in .net

I was reading about Value types and Reference Types for the exam 70-536 which I will be giving soon and there were certain things that I already knew already but some of them were new to me and hence I thought I would have a summary post of what I understand of value types and reference types in .net.

Value Type:

The .net CLR(Common Language Runtime) store the data of the value types in a part of the memory called as the stack. The most basic type of variables like Integers, Booleans, Bytes etc are stored as value types by the .net runtime. Value types can be easily created, updated and removed from the stack by the .net runtime.
There are three types of value types:
•    The built-in types of the .net framework
•    The user defined types
•    Enumerations.

Reference Types:

The .net CLR stores reference types in 2 steps; firstly the address of the memory location, called as a pointer is stored in the stack. This pointer points to the address where the actual data is stored in the part of the memory called as the heap. Reference types which are no longer referenced are garbage collected periodically by the runtime. Reference types are generally very effective in passing parameters to functions.

Some of the examples of reference types include strings, objects (of classes; built-in or otherwise) etc. To illustrate more, I did an experiment which demonstrates value type and reference type and how can it be detected if a variable is a value type or a reference type.

SByte a = 0;
Byte b = 0;
Int16 c = 0;
Int32 d = 0;
Int64 e = 0;
string s = “”;
Exception ex = new Exception();
object[] types = { a, b, c, d, e, s, ex };
foreach (object o in types)
{
string value = “”;
if (o.GetType().IsValueType)
{
value = “Value type”;
}
else
{
value = “Reference type”;
}
Console.WriteLine(“{0},{1}”, o.GetType(), value);
}

In my next post, I will write about how to create your own value type and an example of a reference type used for function calls.

This entry was posted in .NET and tagged , . Bookmark the permalink.

17 Responses to Value Types and Reference Types in .net

  1. Thanks for writing, I really enjoyed reading your latest post. I think you should post more often, you clearly have talent for blogging!

  2. WP Themes says:

    Nice fill someone in on and this enter helped me alot in my college assignement. Gratefulness you seeking your information.

  3. very good information you write it very clean. I’m very lucky to get this information from

    you.

  4. thanks for great informations It’s a wonderful

  5. Thanks for sharing this.

  6. Great post, I learned a lot! Looking forward to checking back soon!

  7. Katheryn says:

    Great post, bookmarked your website for interest to see more!

  8. Really nice and impressive blog i found today.

  9. great share, great article, very usefull for me…thank

    you

  10. pu erh says:

    wow.. i’m very

    enjoy reading your post. great.

  11. Greetings from a keen reader. Well, there have been many pieces of writing that I have read today however none of them compares to this. Well done! And I want you to know that my mates consider me intensely critical therefore that is lofty praise indeed.

  12. illelaype says:

    Very Interesting!
    Thank You!

  13. Nella Luckow says:

    This site seems to get a large ammount of visitors. How do you advertise it? It offers a nice individual spin on things. I guess having something useful or substantial to talk about is the most important factor.

  14. Informative story, saved your blog with hopes to read more information!

  15. Lue Rameriez says:

    Hey, i think you visited my website so i came to “return the favour”.I am looking for ways to add things to my blog!Is it ok if i use some of the things i saw here?!

  16. pro stones says:

    i’m adding your blog rss feed so that I can see your new posts. Keep up the good work!

  17. Pete Valerie says:

    Hey! Impressive write-up! I am just an everyday visitor (a great deal more like addict ) on this website sadly I’d an issue. I will be only not actually absoluetly certain if it is the right spot must, but you haven’t any spam comments. I receive comments everyday. Might you assist me? : )!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>