{"id":4107,"date":"2014-03-30T07:43:27","date_gmt":"2014-03-30T07:43:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-the-new-keyword-on-existing-objects-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:43:27","modified_gmt":"2014-03-30T07:43:27","slug":"c-the-new-keyword-on-existing-objects-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-the-new-keyword-on-existing-objects-collection-of-common-programming-errors\/","title":{"rendered":"C# The &#39;new&#39; keyword on existing objects-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p><em>Since the reference was reused<\/em>, does the garbage collector dispose \/ handle the &#8216;Red Car&#8217; after it&#8217;s lost it&#8217;s reference?<\/p>\n<\/blockquote>\n<p>You&#8217;re looking at this in perhaps the wrong way:<\/p>\n<pre><code>c [*] ----&gt; [Car { Name = \"Red Car\" }]  \/\/ Car c = new Car(\"Red Car\")\n<\/code><\/pre>\n<p>Then your next step:<\/p>\n<pre><code>c [*]       [Car { Name = \"Red Car\"  }] \/\/ No chain of references to this object\n   \\------&gt; [Car { Name = \"Blue Car\" }] \/\/ c = new Car(\"Blue Car\")\n<\/code><\/pre>\n<p>The GC will come along and &#8220;collect&#8221; any of these objects which have no chain of references to a live object at some point in the future. For most tasks, as long as you&#8217;re using <em>managed<\/em> data, you should not worry about large objects versus small objects.<\/p>\n<p>For most tasks you only worry about <em>deterministic<\/em> memory management when dealing with <code>IDisposable<\/code>. As long as you follow the best practice of <code>using<\/code>-blocks, you will generally be fine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since the reference was reused, does the garbage collector dispose \/ handle the &#8216;Red Car&#8217; after it&#8217;s lost it&#8217;s reference? You&#8217;re looking at this in perhaps the wrong way: c [*] &#8212;-&gt; [Car { Name = &#8220;Red Car&#8221; }] \/\/ Car c = new Car(&#8220;Red Car&#8221;) Then your next step: c [*] [Car { Name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4107","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4107"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4107\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}