Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unless you are in a situation where you <strong>must</strong> use .NET 1.0/1.1, or need to interact with legacy code that uses <code>ArrayList</code> - you should really avoid using <code>ArrayLists</code> in new code. Use the <a href="http://msdn.microsoft.com/en-us/library/512aeb7t(VS.80).aspx" rel="noreferrer">generic</a> collection type <code>List&lt;&gt;</code> instead.</p> <p>The operations to add, remove, and replace an item in <code>List&lt;T&gt;</code> are quite straightforward.</p> <p>Let's say you have some <em>hypothetical</em> type <code>Animal</code>, instances of which you will store in a list:</p> <pre><code>Animal dog = new Animal("dog"); Animal cat = new Animal("cat"); List&lt;Animal&gt; animalList = new List&lt;Animal&gt;(); // example of adding items to the list animalList.Add( dog ); animalList.Add( cat ); // example of removing items form the list animalList.Remove( cat ); // example of replacing an item at a given position animalList[0] = new Animal("giraffe"); </code></pre> <p>The public interfaces for <code>List&lt;T&gt;</code> and <code>ArrayList</code> are actually quite similar. The main difference, is that ArrayList can only store <code>object</code> references since it was implemented before .NET supported generics.</p> <pre><code>ArrayList listOfObjects = new ArrayList(); int myAge = 207; listOfObjects.Add( (object)myAge ); </code></pre> <p>In the example above, you <em>MUST</em> cast types like <code>int</code> (<a href="http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx" rel="noreferrer">which are value types in .NET</a>) to object. This results in a boxing conversion - which copies the int value type to a new location on the heap, and passes it to ArrayList. <a href="http://msdn.microsoft.com/en-us/library/25z57t8s(VS.71).aspx" rel="noreferrer">Boxing conversions</a>, are one of the disadvantages of using <code>ArrayList</code> - <code>List&lt;T&gt;</code> avoids this by virtue of being a <a href="http://msdn.microsoft.com/en-us/library/512aeb7t(VS.80).aspx" rel="noreferrer">generic class</a>. Another issue is that <code>ArrayList</code> does not prevent you from mixing different types in the list together. For instance:</p> <pre><code>listOfObjects.Add( (object)myAge ); listOfObjects.Add( "Hello World" ); </code></pre> <p>are both allowed. However, when accessing elements of an <code>ArrayList</code>, you must <em>know</em> what type you are trying to retrieve. This makes <code>ArrayList</code> more fragile as a collection type, because the caller must write code to either protect themselves from arbitrary types being stored in the <code>ArrayList</code>, or else use reflection and runtime type checks to convert the values being stored. <code>List&lt;T&gt;</code> avoids both of these problems by allowing the <em>compiler</em> to help verify that only appropriate types are stored in the collection (those that match the type parameter <code>T</code> in <code>List&lt;T&gt;</code>).</p> <p>There's a great deal more that could be written about interacting with collections - and in fact there is. Here's a <a href="http://rads.stackoverflow.com/amzn/click/1590599543" rel="noreferrer">link to just one</a> of many great books on the subject. <strong>My advice would be, before you begin writing code in .NET/C#, you should take the time to <a href="http://msdn.microsoft.com/en-us/library/aa645597(VS.71).aspx" rel="noreferrer">familiarize yourself with the basic concepts of the C# language and type system</a></strong> - what are reference vs. value types. What are primitives. What are generics. etc. This will help ensure that when you start writing code, the code does what you need it to do. C# has a sophisticated and rich type system- as well as a vast library of framework classes. It's important to have a good grounding in the core aspects of the language before you get too deep into writing actual code. Examples like those I show above will only get you so far - and they already introduce numerous language concepts: variables, constructors, generics, boxing conversions, etc.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload