{"id":126,"date":"2014-04-23T11:36:00","date_gmt":"2014-04-23T10:36:00","guid":{"rendered":"https:\/\/debuggersspace.com\/index.php\/2014\/04\/23\/difference-between-a-value-type-and-a-reference-type\/"},"modified":"2014-04-23T11:36:00","modified_gmt":"2014-04-23T10:36:00","slug":"difference-between-a-value-type-and-a-reference-type","status":"publish","type":"post","link":"https:\/\/debuggersspace.com\/index.php\/2014\/04\/23\/difference-between-a-value-type-and-a-reference-type\/","title":{"rendered":"Difference between a Value Type and a Reference Type?"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>2 Minute, 54 Second                <\/div>\n\n            <\/div><div dir=\"ltr\" style=\"text-align: left;\">\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">The Types in .NET Framework are either treated by Value Type or by Reference Type. A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data. Reference Type variables are stored in the heap while Value Type variables are stored in the stack.<\/span><\/div>\n<div>\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><br \/><\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><b>In a simple sentance<\/b> the Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data.<\/span><\/div>\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"color: blue; font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><b>Value Type:<\/b><\/span><\/div>\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">A Value Type stores its contents in memory allocated on the stack. When you created a Value Type, a single space in memory is allocated to store the value and that variable directly holds a value. If you assign it to another variable, the value is copied directly and both variables work independently. Predefined datatypes, structures, enums are also value types, and work in the same way. Value types can be created at compile time and Stored in stack memory, because of this, Garbage collector can&#8217;t access the stack.<\/span><\/div>\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">e.g.<\/span><span style=\"color: blue; font-family: &quot;times&quot; , &quot;times new roman&quot; , serif; text-align: left;\">&nbsp;int x = 10;<\/span><\/div>\n<div style=\"background-color: white; color: blue; height: auto; overflow-x: auto; width: 580px;\">\n<div align=\"justify\">\n<\/div>\n<\/div>\n<div style=\"background-color: white;\">\n<\/div>\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">Here the value 10 is stored in an area of memory called the stack.<\/span><\/div>\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><br \/><\/span><\/div>\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"color: blue; font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><b>Reference Type:<\/b><\/span><\/div>\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">Reference Types are used by a reference which holds a reference (address) to the object but not the object itself. Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another doesn&#8217;t copy the data. Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value. Reference Type variables are stored in a different area of memory called the heap. This means that when a reference type variable is no longer used, it can be marked for garbage collection. Examples of reference types are Classes, Objects, Arrays, Indexers, Interfaces etc.<\/span><\/div>\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">e.g.<\/span><span style=\"color: blue; font-family: &quot;times&quot; , &quot;times new roman&quot; , serif; text-align: left;\">&nbsp;int[] iArray = new int[20];<\/span><\/div>\n<div style=\"background-color: white; color: blue; height: auto; overflow-x: auto; width: 580px;\">\n<div align=\"justify\">\n<\/div>\n<\/div>\n<div style=\"background-color: white;\">\n<\/div>\n<div align=\"justify\" style=\"background-color: white;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">In the above code the space required for the 20 integers that make up the array is allocated on the heap.<\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.<\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><b>Example:<\/b><\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">Reference is usually associated with a pointer. Meaning the memory address where your variable reside is actually holding&nbsp;<b style=\"background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">another memory address<\/b>&nbsp;of the actual object in a different memory location.<\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">Thus, when you do something like:<\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">var a = &#8220;Hello&#8221;;<\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">the computer will do the following:<\/span><\/div>\n<ol style=\"background-color: white; border: 0px; line-height: 17.804800033569336px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1em 30px; padding: 0px; vertical-align: baseline;\">\n<li style=\"background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">allocate memory (say starting at memory location 1000 for 5 bytes) and put H (at 1000), e (at 1001), l (at 1002), l (at 1003) and o (at 1004).<\/span><\/li>\n<li style=\"background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">allocate somewhere in memory (say at location 0500) and assigned it as the variable a.<br \/>So it&#8217;s kind of like an alias (0500 is a).<\/span><\/li>\n<li style=\"background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">assign the value at that memory location (0500) to 1000 (which is where the string Hello start in memory). Thus the variable a is holding a&nbsp;<b style=\"background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">reference<\/b>&nbsp;to the actual starting memory location of the &#8220;Hello&#8221; string.<\/span><\/li>\n<\/ol>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">Value type will hold the actual thing in its memory location.<\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">Thus, when you do something like:<\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">var a = 1;<\/span><\/div>\n<div style=\"background-color: white; border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">the computer will do the following:<\/span><\/div>\n<ol style=\"background-color: white; border: 0px; line-height: 17.804800033569336px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1em 30px; padding: 0px; vertical-align: baseline;\">\n<li style=\"background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">allocate a memory location say at 0500 and assign it to variable a (the same alias thing)<\/span><\/li>\n<li style=\"background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">put the value 1 in it (at memory location 0500).<br \/>Notice that we are not allocating extra memory to hold the actual value (1). Thus a is actually holding the&nbsp;<b style=\"background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">actual value<\/b>&nbsp;and that&#8217;s why it&#8217;s called value type.<\/span><\/li>\n<\/ol>\n<div>\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><span style=\"line-height: 17.804800033569336px;\">&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/span><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><span style=\"line-height: 17.804800033569336px;\"><a href=\"http:\/\/www.codeproject.com\/Articles\/32029\/Reference-and-Value-Types-in-C\" target=\"_blank\" rel=\"noopener\">http:\/\/www.codeproject.com\/Articles\/32029\/Reference-and-Value-Types-in-C<\/a><\/span><\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Types in .NET Framework are either treated by Value Type or by Reference Type. A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data. Reference Type variables are stored in the heap while Value Type variables are [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[24,22],"tags":[],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-c-net","category-l-t"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"The Types in .NET Framework are either treated by Value Type or by Reference Type. A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data. Reference Type variables are stored in the heap while Value Type variables are&hellip;","_links":{"self":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/126"}],"collection":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":0,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"wp:attachment":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}