<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[C#] arrays</title>
		<link>http://www.allegro.cc/forums/view/596440</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 27 May 2008 22:34:33 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I just recently started learning C# (to try out XNA, of course <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />), and I&#39;ve a couple of questions, which I couldn&#39;t find answers to anywhere in the documentation.</p><p>1. Is there something like vector&lt;&gt; in C#? If so, what is it and how do I use it?<br />2. Do I need to somehow destroy the objects in such vector (I&#39;m asking because C# doesn&#39;t use pointers), or do they get deleted automatically?</p><p>Mostly, I just want a vector&lt;&gt; or something like that, because arrays aren&#39;t the best choice for things like bullets etc.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CursedTyrant)</author>
		<pubDate>Thu, 22 May 2008 22:31:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
1. Is there something like vector&lt;&gt; in C#? If so, what is it and how do I use it?
</p></div></div><p>Yes, see <a href="http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx">System.Collections.Generic</a>, particularly the <a href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx">List</a> class.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
2. Do I need to somehow destroy the objects in such vector (I&#39;m asking because C# doesn&#39;t use pointers), or do they get deleted automatically?
</p></div></div><p>All objects are garbage collected automatically, although there are some objects, those which implement the <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx">IDisposable</a> insterface, which need to be disposed manually. You can use a <a href="http://msdn.microsoft.com/en-us/library/yh598w02.aspx">using statement</a> to help with the process.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Thu, 22 May 2008 22:37:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks! That should make things much more easier <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CursedTyrant)</author>
		<pubDate>Thu, 22 May 2008 23:01:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>As another C# beginner, I got a question about Dispose() <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>Is there some generic rule about which objects implement the IDisposable interface?<br />Or is it something I have to check myself for each object I plan to use in the future?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ixilom)</author>
		<pubDate>Sat, 24 May 2008 15:22:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Any object that opens an expensive handle will generally need to be IDisposable. For example, database connections, network connections, window handles, etc. Your images may, depending (I&#39;m not familiar with XNA), as might your textures.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Sat, 24 May 2008 19:57:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Wait, you have to manually dispose of IDisposable objects? Isn&#39;t that a little backwards? I don&#39;t get it to be honest, why can&#39;t the GC handle calling the destructors properly (where complex objects clean up after them selves)?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 24 May 2008 23:21:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Because the GC might not run for a week depending on the conditions of your application. Also, the GC doesn&#39;t know how to close a database connection or window handle, it can only forget about it.</p><p>The .NET runtime correctly handles destructors, but it has some limitations (obviously, it requires a GC run). For more information, see <a href="http://msdn.microsoft.com/en-us/library/system.object.finalize.aspx">Object.Finalize</a>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Sat, 24 May 2008 23:28:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So basically... If I had a base class <tt>CObject</tt>, and two classes <tt>CEnemy</tt> and <tt>CEnemy2</tt>, how would this work?:
</p><div class="source-code snippet"><div class="inner"><pre>CObject<span class="k2">[</span><span class="k2">]</span> aObjects <span class="k3">=</span> <span class="k1">new</span> CObject<span class="k2">[</span><span class="n">5</span><span class="k2">]</span><span class="k2">;</span> <span class="c">//random number, really</span>

aObjects<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> CEnemy<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
aObjects<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> CEnemy2<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Does <tt>CEnemy</tt> get lost in memory, deleted, replaced by <tt>CEnemy2</tt> or what? Is it safe/efficient to do it this way, or should I do it differently?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CursedTyrant)</author>
		<pubDate>Tue, 27 May 2008 03:01:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>IDisposable really is for things like DB connections, and things that acquire native resources (like Win32 handles) that need to be freed. Normal object creation and deletion is handled by the GC.</p><p>In your example, the original CEnemy object would be collected by the GC eventually, quite possibly never if you don&#39;t allocate enough memory. In short-lived programs in managed environments like Java, it&#39;s possible that the GC never runs and objects are never deleted, letting the memory be freed by the OS. I assume the same is true for .NET.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Tue, 27 May 2008 06:09:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre>CObject<span class="k2">[</span><span class="k2">]</span> aObjects <span class="k3">=</span> <span class="k1">new</span> CObject<span class="k2">[</span><span class="n">5</span><span class="k2">]</span><span class="k2">;</span>
aObjects<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> CEnemy<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
aObjects<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> CEnemy2<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Actually, The first line already allocates 5 CObjects.<br />The second line loses the reference to aObjects[0], making it elligible for garbage collection.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Tue, 27 May 2008 13:42:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre>Object<span class="k2">[</span><span class="k2">]</span> objects <span class="k3">=</span> <span class="k1">new</span> Object<span class="k2">[</span><span class="n">5</span><span class="k2">]</span><span class="k2">;</span>
objects<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> Enemy<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
objects<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> Enemy<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Actually, The first line already allocates 5 CObjects.
</p></div></div><p>
Wrong.</p><p>The first line creates an array of objects, they are not yet initialized to anything.<br />Second line allocates a new instance and assigns it to the reference.<br />Third line reassigns the reference, making the previous instance eligible for GC.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (monkeyCode)</author>
		<pubDate>Tue, 27 May 2008 14:14:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Argh you&#39;re right... CObject isn&#39;t a value type, so the array will only be initialized to null &quot;pointers&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Tue, 27 May 2008 15:10:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>null references <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /> (Yes, nitpicking here <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (monkeyCode)</author>
		<pubDate>Tue, 27 May 2008 22:34:33 +0000</pubDate>
	</item>
</rss>
